How to validate multiple emails using Regex?

后端 未结 3 1699
心在旅途
心在旅途 2021-01-13 16:02

After a quick research on the Stackoverflow, I wasn\'t able to find any solution for the multiple email validation using regex (split JS function is not applicable, but some

3条回答
  •  旧巷少年郎
    2021-01-13 16:42

    var email = "[A-Za-z0-9\._%-]+@[A-Za-z0-9\.-]+\.[A-Za-z]{2,4}";
    var re = new RegExp('^'+email+'(;\\n*'+email+')*;?$');
    
    [ "john@smith.com;john@smith.com",
      "john@smith.com;john@smith.com;",
      "john@smith.com;\njohn@smith.com;\njjoh@smith.com",
      "john@smith.com jackob@smith.com",
      "jackob@smith.com,",
      "daniels@mail.com\nsmth@mail.com" ].map(function(str){
        return re.test(str);
    }); // [true, true, true, false, false, false]
    

提交回复
热议问题