Reputation: 3683
I m just learning regular expression matching using javascript, and have a doubt regarding the scenario described below.
I'm trying to validate email, with condition below:
Format should be
[email protected]
where
A) "xxx", "yyy" and "zzz" parts can take values only between lowercase a and z
B) The length of zzz, yyy and xxx parts are arbitrary (should be minimum one though)
Now I understand I can build the regex like this:
EDIT:
CORRECTED REG EXP
/[a-z]+@[a-z]+\.[a-z]+/
and the above would evaluate a string like "[email protected]" as true.
But my concern is, in the above expression if i provide "[email protected]"
, again it would evaluate as true. Now, if i modify the reg ex as **/[a-z]{1,}@[a-z]+\.[a-z]+/**
even then it would evaluate "[email protected]"
as true because of the presence of "a" as the first character.
So, I would like to know how to match the first part "xxx" in the email "[email protected]"
in a way that it checks the entire string from first char till it reaches the @ symbol with the condition that it should take only a to z as valid value.
In other words, the regex should not mind the length of the username part of email, and irrespective of the number of chars entered, it should test it based on the specified regex condition, and it should test it for the set of chars from index 1 to index of @.
Upvotes: 5
Views: 4107
Reputation: 37543
Your regular expression will not show [email protected]
to be a match. It will, however, show that [email protected]
is a match. The regular expression markers you're missing are ^
for the start of string and $
for end of string. This is what you're looking for:
/^[a-z]{1,}@[a-z]+\.[a-z]+$/
You can test this expression out over on this online validator: http://tools.netshiftmedia.com/regexlibrary/#
Upvotes: 3
Reputation: 5786
/[a-z]+@[a-z]\.[a-z]+/
will not match "[email protected]", but will match "[email protected]". You are only allowing for a single lowercase letter between the @ and the .
Again, the string "[email protected]" will not match. [a-z]+ means look for one or more characters between a and z... digits are not included, so 999 wil not be matched.
does the regex
/[a-z]+@[a-z]+/.[a-z]+/
do what you want?
Upvotes: 3