Regex for email matching

前端 未结 5 1121
忘掉有多难
忘掉有多难 2020-12-11 08:54

I am using this regex to match email addresses in a string.

Everything works fine here: http://regexr.com?31e5a with this regex:

([\\w-\\.]+)@((?:[\         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-11 09:33

    Your immediate problem (by your own comments) is that you are using capturing groups in you regex, where you should be using non-capturing ones, or better, none at all:

    emailsString = 'aaaaaaa@bbbb.com xxxxxxx cccccc@ffffdd.com';
    emails = emailsString.match(/[\w-\.]+@[\w]+\.+[a-zA-Z]{2,4}/g);
    // emails = ["aaaaaaa@bbbb.com", "cccccc@ffffdd.com"]
    

提交回复
热议问题