简体   繁体   中英

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 basic 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

  1. xx matches as two non dot characters.
  2. .x matches as an optional dot followed by a non-dot.
  3. x. matches as a non-dot followed by an optional dot at the end.
  4. .. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM