从长字符串中过滤数据(vcard) [英] Filter data out of long string (vcard)

查看:276
本文介绍了从长字符串中过滤数据(vcard)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从vcard QR码扫描数据。我收到的字符串看起来像这样:

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
TITLE:Project Engineer
TEL;WORK:+32 (0)11 12 13 14
ADR;WORK:Industrielaan 1;2250 Olen;Belgium
EMAIL:link.com
URL:http://www.link.com
END:VCARD

我需要一些数据来自动填写表格(我在 jQuery 中这样做)。我需要名字姓氏组织电话号码

I need some data to automatically fill in the form (I'm doing this in jQuery). I need the firstname, lastname, organisation and telephone number.

所以我需要 N ORG 电话之后的数据。但我真的很困惑,我怎么能以最好的方式做到这一点。有这方面的经验,也许有一些提示吗?

So I need the data after N, ORG and TEL. But I'm really stuck on how I could do this the best way. Any experience with this and maybe some tips for me?

更新:

数据有时会有所不同。这些是可能性:

The data varies at times. These are the possibilities:

选项1

BEGIN:VCARD
VERSION:3.0
N:lname;fname;;;
FN:fname lname
TITLE:Project manager
EMAIL;type=INTERNET;type=WORK:s.demesqdqs.be
TEL;type=WORK:+3812788105
END:VCARD

选项2


BEGIN:VCARDFN:Barend VercauterenTEL:+32(0)9 329 93 06EMAIL:Barend.Vercauterenëesc.beURL: http://www.esc.beN:Vercauteren; BarendADR:Grote Steenweg 39; 9840; De PinteORG:ESC bvbaROLE:sales consultantVERSION:3.0END: VCARD

BEGIN:VCARDFN:Barend VercauterenTEL:+32(0)9 329 93 06EMAIL:Barend.Vercauterenëesc.beURL:http://www.esc.beN:Vercauteren;BarendADR:Grote Steenweg 39;9840;De PinteORG:ESC bvbaROLE:sales consultantVERSION:3.0END:VCARD

选项3


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

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

正如您所见,它可能发生所有文本都相互依附..。

As you can see it can happen that all the text is attached to each other .. .

我使用选项1接收正确数据的代码:

My code for receiving the correct data with option 1:

var fname = /FN:(.*)/g;
var org = /ORG:(.*)/g;
var tel = /TEL;[^:]*:(.*)/g;

var fullname, firstname, morg, mtel;

fullname = fname.exec(qr_data);
fullname = fullname[1];
var array = fullname.split(' ');
firstname = array[0];
array.shift();

var lastname = '';

if(array.length > 1){
    $.each(array, function(index, item) {
        lastname += item ;
    });
}
else
{
    lastname = array[0];
}

morg = org.exec(qr_data);
mtel = tel.exec(qr_data);

if(firstname)
{
    $("#firstname").val(firstname);
}

if(lastname)
{
    $("#name").val(lastname);
}

if(morg)
{
    $("#company").val(morg[1]);
}

if(mtel)
{
    $("#number").val(mtel[1]);
}

但是如何使用其他2个选项获取这些数据?

But how can I get these data with the other 2 options?

推荐答案

为了正确解析vCard,您不能依赖单个正则表达式。您可以利用一些vCard解析器。

In order to parse a vCard correctly, you cannot rely on a single regex expression. There are some vCard parsers that you can leverage.

以下是使用 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)); // Names
  console.log("Org: ", JSON.stringify(vcard.org)); // Org
  console.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'
];

更新

这是你可能尝试的正则表达式。但是,它可能不完整,您必须在vCard中获得更多不同的字段名称时进行调整:

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|$)

请参阅演示

第一个捕获组将包含一个字段名称,第二个捕获组将包含字段值。同样,使用专用解析器会更安全。

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/>");
    }
    else if (m[1].toLowerCase() === "org") {
      document.write("Org: " + m[2] + "<br/>");
    }
    else if (m[1].toLowerCase().indexOf("tel") === 0 ||
            m[1].toLowerCase().indexOf("cell") === 0) {
      document.write("Tel.: : " + m[2]);
    }
}

这篇关于从长字符串中过滤数据(vcard)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆