I have this regex
var reg = /^[a-zA-Z0-9._]+@[gmail | yahoo | hotmail]{5,7}.[com | net | org]{3}$/
I want to accept just "gmail hotmail and yahoo" with ".com .net and .org" Is there any method...to set the above regex according to my needs.
Also how to set the range so that it only accept the length of 5 and 7 characters.
The correct syntax for this looks like this:
^[a-zA-Z0-9._]+@(gmail|yahoo|hotmail)\.(com|net|org)$
Squared brackets ([]
) are used to test for testing a character in a group of given characters. Round brackets (()
) are used to test for a particular string (not character) in a group of given strings. You should read the |
as "or".
Examples
[abc]
will match a
or b
or c
.(A1|B2|C2)
will match A1
, B2
or C3
.Additionally, the .
character will be interpreted as "any character" if you don't escape it (\.
), except when being using inside squared brackets.