RegExp,用于将电子邮件列表拆分为基本组件(JavaScript)



我几乎可以工作,但不完全是。

我有一个JavaScript字符串,其中包含每个格式不同的电子邮件列表(没有换行符,为了易读性而编辑):

var emailList = 'peter@pan.com, 
lucky <jack@pot.com>, 
"William Tell" <billy@tell.com>, 
"John Rambo, III" <johnny@rambo.com>, 
"there, might, be, several, commas inside the quotes" <multiple@commas.com>, 
"yes, this is also a valid email address, can you believe" <yes@this@is@valid.com>'

首先,我需要将此字符串拆分为不同的电子邮件。电子邮件按', '分隔:

peter@pan.com, lucky <jack@pot.com>

', '也可能出现在用引号括起来的名称中:

"John Rambo, III" <johnny@rambo.com>

甚至可以在引号中找到多个逗号:

"there, might, be, several, commas inside the quotes" <multiple@commas.com>

第 1 步:替换括在引号中的,

我想用逗号代替类似<<<<!!!!>>>>

我已经尝试过这个正则表达式:(".*)(,)(s.*"), $1<<<<!!!!>>>>$3https://regex101.com/r/baha69/1/但它没有替换引号内的逗号...... :-(

第 2 步:拆分数组并撤消逗号替换

现在,这可以在JavaScript中通过拆分和替换轻松完成:

var Array = emailList.split(', ');
Array.forEach(function(element, index, arr) {
arr[index] = element.replace("<<<<!!!!>>>> ", ", ");
});

在这一点上,我应该有一个这样的数组(没有换行符,为了易读性而编辑):

Array[0] = 'peter@pan.com'
Array[1] = 'lucky
<jack@pot.com>'
Array[2] = '"William Tell"
<billy@tell.com>'
Array[3] = '"John Rambo, III"
<johnny@rambo.com>'
Array[4] = '"there, might, be, several, commas inside the quotes
<multiple@commas.com>'
Array[5] = '"yes, this is also a valid email address, can you believe"
<yes@this@is@valid.com>'

第 3 步:拆分电子邮件地址

现在,我必须将每封电子邮件转换为基本组件(没有换行符,为了便于阅读而编辑):

Array[0] = {fullName: '',
firstWord: '', localPart: 'peter', company: 'pan', 
email: 'peter@pan.com'}
Array[1] = {fullName: 'lucky',
firstWord: 'lucky', localPart: 'jack', company: 'pot', 
email: 'jack@pot.com'};
Array[2] = {fullName: 'William Tell',
firstWord: 'William', localPart: 'billy', company: 'tell',
email: 'billy@tell.com'};
Array[3] = {fullName: 'John Rambo, III',
firstWord: 'John', localPart: 'johnny', company: 'rambo',
email: 'johnny@rambo.com'};
Array[4] = {fullName: 'there, might, be, several, commas inside the quotes', 
firstWord: 'there', localPart: 'multiple', company: 'commas',
email: 'multiple@commas.com'};
Array[5] = {fullName: 'yes, this is also a valid email address, can you believe', 
firstWord: 'yes', localPart: 'yes@this@is', company: 'valid',
email: 'yes@this@is@valid.com'};

为此,我将使用以下正则表达式:

var firstWord = element.match('/"?(w*),? .*"?/ig')[1]; 

这行得通!! :-) https://regex101.com/r/6Z481l/1

var fullName = element.match('/"?(.*)"? </ig')[1]; 

这不起作用:捕获尾随" :-( https://regex101.com/r/6Z481l/2

var localpart = element.match('/<(.*)@/ig')[1];

这不起作用:彼得在peter@pan没有被捕获:-( https://regex101.com/r/6Z481l/3

var company = element.match('/@(.*)./ig')[1];

这行得通!! :-) https://regex101.com/r/6Z481l/4

var email = element.match('/<(.*@.*)>|(^[^<].*[^>])/ig')[1];

令人惊讶的是,这有效!! :-)但我几乎可以肯定它可以变得更优雅 https://regex101.com/r/6Z481l/5

值得一提的是,电子邮件被假定为经过验证

因此,我需要一些帮助来完成步骤 1 和 3。如果步骤 3 中的任何工作正则表达式可以简化或变得更优雅,我会从中学习!

不是目标,但如果你想出一个神奇的正则表达式,像我需要的那样拆分电子邮件,那么我可以保证你肯定会让我惊叹,让我因为缺乏正则表达式知识而感到非常渺小!! :-)

谢谢!

我相信你应该能够使用正则表达式获得预期的最终结果:

(?:(?:"?((w+)b.*b)"?)s)?<?(([w@]*)@(w*).[a-zA-Z]{2,3})>?,?

并将其替换为:

{ fullName:'1', firstWord:'2', localPart:'4', company:'5', email:'3'}

查看演示

您可以在逗号处拆分字符串,不包括括在引号中的字符串,如下所示:

,(?=(?:[^'"]|'[^']*'|"[^"]*")*$)

这应该可以让您摆脱步骤1和2。

关于步骤 3 中的非功能模式:

不起作用:捕获尾随">

  • (?|"([^"]+)"|(.*) <):第一个匹配平衡报价,或者<之前的所有内容。
    警告:如果组 2 为空,则必须检查组 2(不幸的是,JS 没有分支重置组)。

不起作用:peter@pan中的彼得没有被捕获

  • (<|^)(.*)@:你可以从一开始就进行二次匹配;
    但是,这很麻烦,因为模式没有正确锚定。

对于电子邮件验证部分,您应该使用现有和推荐的解决方案之一。但我想这是另一个话题。

最新更新