admin管理员组

文章数量:1134239

I have the following Regular Expression which matches an email address format:

^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$

This is used for validation with a form using JavaScript. However, this is an optional field. Therefore how can I change this regex to match an email address format, or an empty string?

From my limited regex knowledge, I think \b matches an empty string, and | means "Or", so I tried to do the following, but it didn't work:

^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$|\b

I have the following Regular Expression which matches an email address format:

^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$

This is used for validation with a form using JavaScript. However, this is an optional field. Therefore how can I change this regex to match an email address format, or an empty string?

From my limited regex knowledge, I think \b matches an empty string, and | means "Or", so I tried to do the following, but it didn't work:

^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$|\b
Share Improve this question edited Jul 27, 2012 at 9:55 Teun Zengerink 4,3935 gold badges32 silver badges32 bronze badges asked Jul 26, 2010 at 9:04 CurtisCurtis 103k68 gold badges276 silver badges357 bronze badges 4
  • 6 If you must validate an email, be as permissive as possible. You'd be surprised how easy it is to miss real, valid and functional email addresses with home-baked regexes. Your regex, for instance, will fail on these valid addresses: [email protected], [email protected], root@localhost, [email protected]. – Zano Commented Jul 26, 2010 at 9:23
  • 1 Agreeing with Zano, just take a look at this regex ex-parrot.com/pdw/Mail-RFC822-Address.html – Anders Commented Jul 26, 2010 at 9:36
  • @Anders wow, thats a very complex regex! I think I've misunderestimated the complexity of regex – Curtis Commented Jul 26, 2010 at 9:50
  • 5 No, I think you've misunderestimated the complexity of email validation :-) – Zano Commented Jul 26, 2010 at 9:55
Add a comment  | 

5 Answers 5

Reset to default 286

To match pattern or an empty string, use

^$|pattern

Explanation

  • ^ and $ are the beginning and end of the string anchors respectively.
  • | is used to denote alternates, e.g. this|that.

References

  • regular-expressions.info/Anchors and Alternation

On \b

\b in most flavor is a "word boundary" anchor. It is a zero-width match, i.e. an empty string, but it only matches those strings at very specific places, namely at the boundaries of a word.

That is, \b is located:

  • Between consecutive \w and \W (either order):
    • i.e. between a word character and a non-word character
  • Between ^ and \w
    • i.e. at the beginning of the string if it starts with \w
  • Between \w and $
    • i.e. at the end of the string if it ends with \w

References

  • regular-expressions.info/Word Boundaries

On using regex to match e-mail addresses

This is not trivial depending on specification.

Related questions

  • What is the best regular expression for validating email addresses?
  • Regexp recognition of email address hard?
  • How far should one take e-mail address validation?

An alternative would be to place your regexp in non-capturing parentheses. Then make that expression optional using the ? qualifier, which will look for 0 (i.e. empty string) or 1 instances of the non-captured group.

For example:

/(?: some regexp )?/

In your case the regular expression would look something like this:

/^(?:[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+)?$/

No | "or" operator necessary!

Here is the Mozilla documentation for JavaScript Regular Expression syntax.

I'm not sure why you'd want to validate an optional email address, but I'd suggest you use

^$|^[^@\s]+@[^@\s]+$

meaning

^$        empty string
|         or
^         beginning of string
[^@\s]+   any character but @ or whitespace
@         
[^@\s]+
$         end of string

You won't stop fake emails anyway, and this way you won't stop valid addresses.

\b matches a word boundary. I think you can use ^$ for empty string.

^$ did not work for me if there were multiple patterns in regex.

Another solution:

/(pattern1)(pattern2)?/g

"pattern2" is optional. If empty, not matched.

? matches (pattern2) between zero and one times.

Tested here ("m" is there for multi-line example purposes): https://regex101.com/r/mezfvx/1

本文标签: javascriptRegular expression which matches a patternor is an empty stringStack Overflow