【发布时间】:2018-04-21 23:56:45
【问题描述】:
我几乎可以做到这一点,但不完全。
我有一个 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 中的 peter 未被捕获 :-( 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 步中的任何工作正则表达式可以简化或更优雅,我会从中学习!
不是目标,但如果你想出一个神奇的正则表达式,它可以像我需要的那样拆分电子邮件,那么我可以保证你一定会让我赞叹不已,并且因为我缺乏正则表达式知识而让我感到非常渺小! ! :-)
谢谢!
【问题讨论】:
-
var Array =我强烈建议不要隐藏内置的 Array 对象 -
如果不是单个单词,全名是否总是用双引号引起来?
-
@CertainPerformance 好点!!我从来没有这样做过,代码中的变量称为
stringArray,但它对于 SO 易读性来说太长了,我把它缩短了,留下了Array。我的错!
标签: javascript regex split