Filter Data Out Of Long String (vcard)
I'm scanning data from a vcard QR-code. The string I receive always looks something like this: BEGIN:VCARD VERSION:2.1 N:Lastname;Firstname FN:Firstname Lastname ORG:Lol Group TITL
Solution 1:
Use regex
to extract the data.
For name = /FN:(.*)/g
For organization = /ORG:(.*)/g
For telephone = /TEL;[^:]*(.*)/g
Check out this fiddle.
var fname = /FN:(.*)/g;
var org = /ORG:(.*)/g;
var tel = /TEL;[^:]*:(.*)/g;
var str = 'BEGIN:VCARD\nVERSION:2.1\nN:Lastname;Firstname\nFN:Firstname Lastname\nORG:Lol Group\nTITLE:Project Engineer\nTEL;WORK:+32 (0)11 12 13 14\nADR;WORK:Industrielaan 1;2250 Olen;Belgium\nEMAIL:link.com\nURL:http://www.link.com\nEND:VCARD';
var mname, morg, mtel;
mname = fname.exec(str);
morg = org.exec(str);
mtel = tel.exec(str);
alert(mname[1]);
alert(morg[1]);
alert(mtel[1]);
Solution 2:
In order to parse a vCard correctly, you cannot rely on a single regex expression. There are some vCard parsers that you can leverage.
Here is an example of using Nilclass vCardJS:
VCF.parse(input, function(vcard) {
// this function is called with a VCard instance.// If the input contains more than one vCard, it is called multiple times.console.log("Names: ", JSON.stringify(vcard.n)); // Namesconsole.log("Org: ", JSON.stringify(vcard.org)); // Orgconsole.log("Tel: ", JSON.stringify(vcard.tel)); // Tel
});
Here are all defined fields:
VCard.allKeys = [
'fn', 'n', 'nickname', 'photo', 'bday', 'anniversary', 'gender',
'tel', 'email', 'impp', 'lang', 'tz', 'geo', 'title', 'role', 'logo',
'org', 'member', 'related', 'categories', 'note', 'prodid', 'rev',
'sound', 'uid'
];
UPDATE:
Here is a regex that you might try. However, it might not be complete, and you will have to adjust it as you get more different field names in the vCard:
(begin|end|version|cell|adr|nickname|photo|bday|anniversary|gender|tel|email|impp|lang|tz|geo|title|role|logo|org|member|related|categories|note|prodid|rev|sound|uid|fn|n):(.*?)(?=(?:begin|end|version|cell|adr|nickname|photo|bday|anniversary|gender|tel|email|impp|lang|tz|geo|title|role|logo|org|member|related|categories|note|prodid|rev|sound|uid|fn|n):|\n|$)
See demo
The first capturing group will contain a field name and the second will contain the field value. Again, you'd be safer with a dedicated parser.
var re = /(begin|end|version|cell|adr|nickname|photo|bday|anniversary|gender|tel|email|impp|lang|tz|geo|title|role|logo|org|member|related|categories|note|prodid|rev|sound|uid|fn|n):(.*?)(?=(?:begin|end|version|cell|adr|nickname|photo|bday|anniversary|gender|tel|email|impp|lang|tz|geo|title|role|logo|org|member|related|categories|note|prodid|rev|sound|uid|fn|n):|\n|$)/gi;
var str = 'BEGIN:VCARDVERSION:2.1N:Deblieck;Tommy;;DhrFN:Tommy DeblieckTITLE:ZaakvoerderORG:QBMT bvbaADR:;;Kleine Pathoekweg 44;Brugge;West-Vlaanderen;8000;Belgi≠A0171TEL;WORK;PREF:+32 479302972TEL;CELL:+32 479302972EMAIL:tdëqbmt.beURL:www.qbis.beEND:VCARD';
var m;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
if (m[1].toLowerCase() === "n") {
document.write("Names: " + m[2] + "<br/>");
}
elseif (m[1].toLowerCase() === "org") {
document.write("Org: " + m[2] + "<br/>");
}
elseif (m[1].toLowerCase().indexOf("tel") === 0 ||
m[1].toLowerCase().indexOf("cell") === 0) {
document.write("Tel.: : " + m[2]);
}
}
Post a Comment for "Filter Data Out Of Long String (vcard)"