RonnyKnoxville
RonnyKnoxville

Reputation: 6394

Javascript email regex

I've come across a description of a decent email regex.

/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

But I would also like to include some punctuation characters into the first part of the email whilst retaining the rest of the functionality (such as no repeated .)

The best I have come up with so far is:

([\/!#$%&'*+-=?^_`{|}~]*\w+)([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,40})+$

But this will not allow punctuation at the end of the first part of the email.

I find regex so confusing, does anyone know how to properly implement this?

Example:

Yes [email protected]
Yes [email protected]
Yes [email protected]
Yes [email protected]
Yes [email protected]
Yes email@[123.123.123.123]
Yes "email"@domain.com
Yes [email protected]
Yes [email protected]
Yes [email protected]
Yes [email protected]
Yes [email protected]
Yes [email protected]
No  plainaddress
No  #@%^%#$@#$@#.com
No  @domain.com
No  Joe Smith <[email protected]>
No  email@[email protected]
No  [email protected]
No  [email protected]
No  [email protected]
No  あいうえお@domain.com
No  [email protected] (Joe Smith)
No  email@domain
No  [email protected]
No  [email protected]

Upvotes: 0

Views: 407

Answers (3)

Yogesh Chauhan
Yogesh Chauhan

Reputation: 348

([a-zA-Z0-9\-\_\"]+)([\_\.\-\+{1}])?([a-zA-Z0-9\-\"\_]+)\@([a-zA-Z0-9\[]+)([\-])?([a-zA-Z0-9]+)?([\.])([a-zA-Z0-9\.\]]+)

Jack, the expression I have mentioned above will provide the solution for this problem. This will give the desired results according to the question.

Upvotes: 0

BenG
BenG

Reputation: 15154

You need to replace \w with its equivalent [A-Za-z0-9_] and escape all the special characters from here.

try this:-

/^[\/\!#\$\%&'\*\+\-\=\?\^_`\{\|\}~A-Za-z0-9]+[\.-]?[\/\!#\$\%&'\*\+\-\=\?\^_`\{\|\}~A-Za-z0-9]*@\w+([\.-]?\w+)*(\.\w{2,40})+$/

Upvotes: 0

RonnyKnoxville
RonnyKnoxville

Reputation: 6394

This is my closest attempt now, I think it is roughly correct

^[a-zA-Z0-9_!#$%&‘*\=\+\/\?^{|}~]+([\.-]?[a-zA-Z0-9_!#$%&‘*\=\+\/\?^{|}~]+)*@\w+([\.-]?\w+)*(\.\w{2,50})+$

Upvotes: 1

Related Questions