求一条正则,需求有点复杂,可能需要加脚本辅助。
大概需求:
匹配一段字符串,字符串由email地址组成,可能有合法的地址和非法地址,需要将合法和非法的都匹配出来,我说一下合法的地址,合法地址包括这些:
一般常用的格式:[email protected]
特殊地址格式:"yyy"<[email protected]>, zzz<[email protected]>
以上xxx.com必须匹配为正确的域名,yyy可以为任何字符,zzz为除了空格和;,以外的字符输入地址可能由多个email地址组成,由","或者";"分隔,要求结果匹配出每条的合法和非法的地址,并且合法的地址当是特殊的地址时,必须获取到yyy或者zzz和[email protected]部分例如传入的字符串是:[email protected],"小 明"<[email protected]>;错误;小雪<[email protected]>;wrong.com
要求返回的结果是:
{
match : [
{name : "", address : "[email protected]"},
{name : "小 明", address : "[email protected]"},
{name : "小雪", address : "[email protected]"}
],
wrong : ["错误", "wrong.com"]
}
大概需求:
匹配一段字符串,字符串由email地址组成,可能有合法的地址和非法地址,需要将合法和非法的都匹配出来,我说一下合法的地址,合法地址包括这些:
一般常用的格式:[email protected]
特殊地址格式:"yyy"<[email protected]>, zzz<[email protected]>
以上xxx.com必须匹配为正确的域名,yyy可以为任何字符,zzz为除了空格和;,以外的字符输入地址可能由多个email地址组成,由","或者";"分隔,要求结果匹配出每条的合法和非法的地址,并且合法的地址当是特殊的地址时,必须获取到yyy或者zzz和[email protected]部分例如传入的字符串是:[email protected],"小 明"<[email protected]>;错误;小雪<[email protected]>;wrong.com
要求返回的结果是:
{
match : [
{name : "", address : "[email protected]"},
{name : "小 明", address : "[email protected]"},
{name : "小雪", address : "[email protected]"}
],
wrong : ["错误", "wrong.com"]
}
var obj = {match:[],wrong:[]};var reg = /(?:"([^",;]*)"<)?(\w+@\w+\.com)>?/
for (var i=0; i<arr.length; i++)
{
if (reg.test(arr[i]))
obj.match.push({name:RegExp.$1,address:RegExp.$2})
else
obj.wrong.push(arr[i]);
}alert(JSON.stringify(obj));
var json = {match : [], wrong : []};
this.replace(/[^,|;]+(?=,|;|$)/g, function($) {
var obj = {}, reg = /^"?([^;|,|<|"]*)?["<]*(\b\w+@\w+(\.[a-zA-z]+){1,2})>*$/;
reg.test($) && (obj.name = RegExp.$1, obj.address = RegExp.$2) &&
json.match.push(obj) || json.wrong.push($);
})
return json
}alert('[email protected],"小 明"<[email protected]>;错误;小雪<[email protected]>;wrong.com'.chkMail().match.length)</script>
var json = {match : [], wrong : []};
this.replace(/[^,|;]+(?=,|;|$)/g, function($) {
var obj = {}, reg = /^"?([^;|,|<|"]*)?["<]*(\b\w+@\w+(\.[a-zA-z]+){1,2})>*$/;
reg.test($) && (obj.name=RegExp.$1, obj.address=RegExp.$2, json.match.push(obj)) || json.wrong.push($);
})
return json
}alert('[email protected],"小 明"<[email protected]>;错误;小雪<[email protected]>;wrong.com'.chkMail().match[0].address)</script>
你的代码不能将小雪的name匹配出来。to:prototyper
你的代码很简介,果然基本一行代码可以实现,在我看来你已经是个强人,不过有个地方需要改一下,当地址的name部分有引号时,引号里面可以包括“,;”字符的,比如:"小,明"<[email protected]>是合法的。
我刚看你你的回复,一时还没想到理想的匹配模式,也许换个思路会简单的多。毕竟,正则的要义是简洁。<script type=text/javascript>String.prototype.chkMail = function() {
var json = {match : [], wrong : []};
this.replace(/(".*)(,|;)(.*")/g, "$1 $3").replace(/[^;,]+(?=,|;|$)/g, function($) {
var obj = {}, reg = /^"*([^<"]+)?["<]*(\b\w+@\w+(\.[a-zA-z]+){1,2})>*$/;
reg.test($) && (obj.name=RegExp.$1, obj.address=RegExp.$2, json.match.push(obj)) || json.wrong.push($);
})
return json
}alert('[email protected],"小; 明"<[email protected]>;错误;小雪<[email protected]>;wrong.com'.chkMail().match[2].name)</script>
不好意思,没注意意""可省:
var reg = /(?:"?([^",;]*)"?<)?(\w+@\w+\.com)>?/我的思路是先按[,;]拆分,再验证email格式。拆分那个正则考虑的比较复杂,要排除"yyy"里面的,;,如果没有这种情况,直接[,;]拆分就可以了。
function trimString(str) {
return str.replace(/^[<"]|[>"]$/g,"");
}
var str = '[email protected],"小 明"<[email protected]>;错误;小雪<[email protected]>;wrong.com';
var o = {
match:[],
wrong:[]
};
var strarr = str.split(/[,;]/g);
var reg = /^(?:("[^"]+"|[^<]*)(?=<))?(<\w+@\w+(?:\.\w+){1,2}>|\w+@\w+(?:\.\w+){1,2})$/i;
for(var i in strarr){
if(strarr[i].match(reg)) {
o.match.push({
name:trimString(RegExp.$1),
address:trimString(RegExp.$2)
});
}
else
o.wrong.push(strarr[i]);
}
var str = '[email protected],"小 ;明"<[email protected]>;错误;小雪<[email protected]>;wrong.com,"小, ;,王"<[email protected]>'; //加了条小王
var array = str.split(/[,;](?![^"]+"<)/g);
var json = {match : [], wrong : []};
for(var i=0;i<array.length;i++){
if(/(?:"?([^"\n]*?)"?<)?(\w+@[\w.]+)/.exec(array[i])){
json.match.push({name:RegExp.$1,address:RegExp.$2});
}
else{
json.wrong.push(array[i]);
}
}
"王明<[email protected]>
或者aaa<[email protected]
这样的
嗯,[,;](?![^"]+"<)好。<script type=text/javascript>String.prototype.chkMail = function() {
var json = {match : [], wrong : []};
this.replace(/[,;]+(?![^"]+"<)/g, "§").replace(/[^§]+(?=§|$)/g, function($) {
reg = /^"*([^<"]+)?["<]*(\b\w+@\w+(\.[a-zA-z]+){1,4})[>,;]*$/;
reg.test($) && json.match.push({name:RegExp.$1, address:RegExp.$2}) || json.wrong.push($);
})
return json
}var str = '[email protected],"小 ;明"<[email protected]>;错误;小雪<[email protected]>;wrong.com,"小, ;,王"<[email protected]>';
alert(str.chkMail().match[3].name)</script>
var json = {match : [], wrong : []}, rAddr = "(\\b\\S+@([\\w\\-\\_]+\\.)+[\\w\\-]{2,7})";
this.replace(/[,;]+(?![^"]+"<)/g, "§").replace(/[^§]+(?=§|$)/g, function($) {
if(/[<>]/g.test($)){
if(/\"/g.test($)){
reg = new RegExp('^"([^<"]+)?"<'+ rAddr +'>$');
}else{
reg = new RegExp('^([^<"]+)?<'+ rAddr +'>$');
}
}else{
reg = new RegExp('^(^)'+ rAddr +'$');
}
reg.test($) && json.match.push({name:RegExp.$1, address:RegExp.$2}) || json.wrong.push($);
})
return json
}
alert(JSON.stringify('[email protected],"xyz"<[email protected]>;"xyz<[email protected]>;"xyz"[email protected]>;"xyz"<ab [email protected]>;"小;,小;,明"<[email protected]>;错误;小雪<[email protected]>;wrong.com'.chkMail()))
[code=JScrip]
<script type=text/javascript>String.prototype.chkMail = function() {
var json = {match : [], wrong : []};
this.replace(/("[^"]+"<)?[^,;]+/g, function($) {
var reg = /^"*([^<"]+)?["<]*(\b\w+@\w+(\.[a-zA-z]+){1,4})[>,;]*$/;
reg.test($) && json.match.push({name:RegExp.$1, address:RegExp.$2}) || json.wrong.push($);
})
return json
}var str = '[email protected],"小 ;明"<[email protected]>;错误;小雪<[email protected]>;wrong.com,"小, ;,王"<[email protected]>';
alert(str.chkMail().match[3].name)</script>
[/code]
这样,就省去了一个replace(/[,;]+(?![^"]+"<)/g, "§")的替换过程,提高了效率,舒服了代码。再次改写LZ的100分!
<script type=text/javascript>String.prototype.chkMail = function() {
var json = {match : [], wrong : []};
this.replace(/("[^"]+"<)?[^,;]+/g, function($) {
var reg = /^"*([^<"]+)?["<]*(\b\w+@\w+(\.[a-zA-z]+){1,4})[>,;]*$/;
reg.test($) && json.match.push({name:RegExp.$1, address:RegExp.$2}) || json.wrong.push($);
})
return json
}var str = '[email protected],"小 ;明"<[email protected]>;错误;小雪<[email protected]>;wrong.com,"小, ;,王"<[email protected]>';
alert(str.chkMail().match[3].name)</script>
[/code]