Email Regular Expression - Excluded Specified Set
I have been researching a regular expression for the better part of about six hours today. For the life of me, I can not figure it out. I have tried what feels like about a hundred different approaches to no avail. Any help is greatly appreciated!
The bas开发者_开发问答ic rules:
1 - Exclude these characters in the address portion (before the @ symbol): "()<>@,;:\[]*&^%$#!{}/"
2 - The address can contain a ".", but not two in a row.
I have an elegant solution to the rule number one, however, rule number two is killing me! Here is what I have so far. (I'm only including the portion up to the @ sign to keep it simple). Also, it is important to note that this regular expression is being used in JavaScript, so no conditional IF is allowed.
/^[^()<>@,;:\\[\]*&^%$#!{}//]+$/
First of all, I would suggest you always choose what characters you want to allow instead of the opposite, you never know what dangerous characters you might miss.
Secondly, this is the regular expression I always use for validating emails and it works perfectly. Hope it helps you out.
/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i
Rule number 2
/^(?:\.?[^.])+\.?$/
which means any number of sequences of (an optional dot followed by a mandatory non dot) with an optional dot at the end.
Consider four two character sequences
xx
matches as two non dot characters..x
matches as an optional dot followed by a non-dot.x.
matches as a non-dot followed by an optional dot at the end...
does not match because there is no non-dot after the first dot.
One thing to remember about email addresses is that dots can appear in tricky places
"..@"@.example.com
is a valid email address.
The "..@"
is a perfectly valid quoted local-part production, and .example.com
is just a way of saying example.com
but resolved against the root DNS instead of using a host search path. example.com
might resolve to example.com.myintranet.com
if myintranet.com
is on the host search path but .example.com
always resolves to the absolute host example.com
.
First of all, to your specifications:
^(?![\s\S]*\.\.)[^()<>@,;:\\[\]*&^%$#!{}/]@.*$
It's just your regex with (?!.*\.\.)
tacked onto the front. That's a negative lookahead, which doesn't match if there are any two consecutive periods anywhere in the string.
Properly matching email addresses is quite a bit harder, however.
精彩评论