<pre>BEGIN:VCARD
VERSION:4.0
N:Gump;Forrest;;;
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
PHOTO;MEDIATYPE=image/gif:http://www.example.com/dir_photos/my_photo.gif
TEL;TYPE=work,voice;VALUE=uri:tel:+11115551212
TEL;TYPE=home,voice;VALUE=uri:tel:+14045551212
ADR;TYPE=work;LABEL="100 Waters Edge\nBaytown, LA 30314\nUnited States of A
merica":;;100 Waters Edge;Baytown;LA;30314;United States of America
ADR;TYPE=home;LABEL="42 Plantation St.\nBaytown, LA 30314\nUnited States of
America":;;42 Plantation St.;Baytown;LA;30314;United States of America
EMAIL:[email protected]
REV:20080424T195243Z
END:VCARD</pre>
JavaScript
function parse(input) {
var Re1 = /^(version|fn|title|org):(.+)$/i;
var Re2 = /^([^:;]+);([^:]+):(.+)$/;
var ReKey = /item\d{1,2}\./;
var fields = {};
input.split(/\r\n|\r|\n/).forEach(function (line) {
var results, key;
if (Re1.test(line)) {
results = line.match(Re1);
key = results[1].toLowerCase();
fields[key] = results[2];
} else if (Re2.test(line)) {
results = line.match(Re2);
key = results[1].replace(ReKey, '').toLowerCase();
var meta = {};
results[2].split(';')
.map(function (p, i) {
var match = p.match(/([a-z]+)=(.*)/i);
if (match) {
return [match[1], match[2]];
} else {
return ["TYPE" + (i === 0 ? "" : i), p];
}
})
.forEach(function (p) {
meta[p[0]] = p[1];
});
if (!fields[key]) fields[key] = [];
fields[key].push({
meta: meta,
value: results[3].split(';')
})
}
});
return fields;
};
var $ = function(selector) {
return document.querySelector(selector);
};
/*
$('pre').innerHTML = JSON.stringify(
parse(document.body.innerText), 0, 2
);
*/
var vcard = "BEGIN:VCARD\nVERSION:2.1\nFN:Max Mustermann\nN:Mustermann;Max\nTITLE:Dr.-Ing.\nTEL;CELL:+49 151 62818982\nTEL;WORK;VOICE:+49 123 7654321\nTEL;HOME;VOICE:+49 123 123456\nEMAIL;HOME;INTERNET:[email protected]\nEMAIL;WORK;INTERNET:[email protected]\nURL:http://example.com\nADR:;;MusterstraĂźe 123;Musterstadt;;77777;Deutschland\nORG:ACME Inc.\nEND:VCARD";
console.log(parse(vcard))
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.