admin管理员组

文章数量:1392073

This is a regular expression I found online that seems to do a great job at simple email address validation. The problem is it doesn't allow + in the email, like [email protected]. I'm not great at regex, so how can I add support for a + without breaking the whole thing?

var regex = new RegExp( /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i );

This is a regular expression I found online that seems to do a great job at simple email address validation. The problem is it doesn't allow + in the email, like [email protected]. I'm not great at regex, so how can I add support for a + without breaking the whole thing?

var regex = new RegExp( /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i );
Share Improve this question asked Jun 7, 2018 at 18:10 GavinGavin 8,0164 gold badges57 silver badges77 bronze badges 3
  • See stackoverflow./q/4736/3001761, stackoverflow./q/22937618/3001761 – jonrsharpe Commented Jun 7, 2018 at 18:12
  • I don't think there's any reason to use the RegExp constructor if you're using the /exp/i literal notation. – user47589 Commented Jun 7, 2018 at 18:13
  • Where do you need to support the + sign in the middle or in the start or at any position ? – mostafa tourad Commented Jun 7, 2018 at 18:15
Add a ment  | 

2 Answers 2

Reset to default 6

Change the second [\w-]+ to [+\w-]+

([+\w0-9._-]+@[\w0-9._-]+\.[\w0-9_-]+)

本文标签: javascriptHow can I add support for a(plus sign) in this email validation regexStack Overflow