求一条正则,需求有点复杂,可能需要加脚本辅助。
大概需求:
匹配一段字符串,字符串由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"]
}

解决方案 »

  1.   

    var str = '[email protected],"小 明"<[email protected]>;错误;小雪<[email protected]>;wrong.com';var arr = str.split(/[,;](?=(?:[^"]*"[^"]*")*(?![^"]*"))/)
    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));
      

  2.   

    LZ,我不是版主,也不是强人,按照你的要求(你给出的JSON结构格式和健名)凑合着写了个方法,看是否能用。<script type=text/javascript>String.prototype.chkMail = function() {
         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>
      

  3.   

    再稍微简化下下:<script type=text/javascript>String.prototype.chkMail = function() {
         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>
      

  4.   

    to:Linares
    你的代码不能将小雪的name匹配出来。to:prototyper
    你的代码很简介,果然基本一行代码可以实现,在我看来你已经是个强人,不过有个地方需要改一下,当地址的name部分有引号时,引号里面可以包括“,;”字符的,比如:"小,明"<[email protected]>是合法的。
      

  5.   


    我刚看你你的回复,一时还没想到理想的匹配模式,也许换个思路会简单的多。毕竟,正则的要义是简洁。<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>
      

  6.   


    不好意思,没注意意""可省:
    var reg = /(?:"?([^",;]*)"?<)?(\w+@\w+\.com)>?/我的思路是先按[,;]拆分,再验证email格式。拆分那个正则考虑的比较复杂,要排除"yyy"里面的,;,如果没有这种情况,直接[,;]拆分就可以了。
      

  7.   


    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]);
    }
      

  8.   

    凑个热闹
    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]);
    }
    }
      

  9.   

    之所以写这么麻烦,还是考虑了下一些规则 比如
    "王明<[email protected]>
    或者aaa<[email protected]
    这样的
      

  10.   


    嗯,[,;](?![^"]+"<)好。<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>
      

  11.   

    确实还有这种错误的地址要考虑。先感谢以上各位强人踊跃回复,经过我反复测试各种情况,prototyper的最后修改后的代码基本很完美了,再加上上面的两种错误类型判断,似乎比较难,我刚刚改为以下不太简洁的代码,不过也比现在正在用的恶心的实现方式强多了,大家看看还可不可以继续优化一下:String.prototype.chkMail = function() {
         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()))
      

  12.   

    感谢LZ给我了100分,这是我得到的最高可用分了,后来我想到了较为理想的匹配模式,外去未上网忘记贴上来,刚想起补充如下:
    [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分!
      

  13.   

    [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]