I want it to be correct my JavaScript regex pattern to validate below email address scenarios
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- msekhar#[email protected]
- \u001\[email protected]
I have tried the following regex pattern but it's not validating all the above scenarios
/^[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/
Could any one please help me with this where am doing wrong?
I want it to be correct my JavaScript regex pattern to validate below email address scenarios
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- msekhar#[email protected]
- \u001\[email protected]
I have tried the following regex pattern but it's not validating all the above scenarios
/^[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/
Could any one please help me with this where am doing wrong?
Share Improve this question edited Apr 5, 2019 at 13:28 sekhar asked Apr 5, 2019 at 9:38 sekharsekhar 1211 gold badge2 silver badges10 bronze badges 4- 1 Seems to be a duplicate question! Did you check the following?- stackoverflow./questions/46155/… – Marcus Commented Apr 5, 2019 at 9:40
- 2 Possible duplicate of How to validate an email address in JavaScript? – jayarjo Commented Apr 5, 2019 at 9:41
- Possible duplicate of JavaScript Regular Expression Email Validation – Shibon Commented Apr 5, 2019 at 9:46
- Note, the the aforementioned referenced topics do not take into account unicode characters in the local part. See also SMTPUTF8 in RFC6531 – Marcus Commented Apr 5, 2019 at 9:53
3 Answers
Reset to default 2The following regex should do:
^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$
Test it: https://regex101./r/7gH0BR/2
EDIT: I have added all your test cases
I have always used this one but note it doesn't trigger on escaped unicode:
^([\w\d._\-#])+@([\w\d._\-#]+[.][\w\d._\-#]+)+$
You can see how it works here: https://regex101./r/caa7b2/4
First off [_a-z0-9]+
is going to match the username fields for the majority of those testcases. Anything further testing of username field content will result in a mismatch. If you write a pattern that expects two .
-delimitered fields, it'll match when you provide two .
-delimitered fields and only then, not anything else. Make a mental note of that. I think you probably meant to put the .
in the first character set, and omit this part here: (.[_a-z0-9]+)
...
As for the domain part of the email address, similar story there... if you're trying to match domains containing two labels (yahoo
and ) against a pattern that expects three... it's going to fail because there's one less label, right? There are domain names that only contain one label which you might want to recognise as email addresses, too, like
localhost
...
You know, there is a point to where you can dig yourself down a very deep rabbit hole trying to parse email addresses, much to the effect of this question and answer sequence. If you're making this plex using regular expressions... I think maybe a better tool is a proper parser generator... otherwise, write the following:
- A pattern that matches anything up until an
@
character - A pattern that matches the
@
character (this will help you learn how to avoid your.
-related error) - A pattern that matches everything (this will help you understand your
.
-related error) - Combine the three above in the order presented.