129

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
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]. 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 Commented Jul 26, 2010 at 9:36
  • @Anders wow, thats a very complex regex! I think I've misunderestimated the complexity of regex Commented Jul 26, 2010 at 9:50
  • 5
    No, I think you've misunderestimated the complexity of email validation :-) Commented Jul 26, 2010 at 9:55

5 Answers 5

291

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


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


On using regex to match e-mail addresses

This is not trivial depending on specification.

Related questions

Sign up to request clarification or add additional context in comments.

1 Comment

I am not great at pattern matching but it looks to me that the above does not match the whole string if the string is not empty. There fore should it be ^$|^pattern$?
16

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.

Comments

11

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.

3 Comments

If an address isn't entered into the field, a NULL value is entered into the database, therefore this can be dealt with when it comes to sending out newsletters etc. I appreciate this won't stop fake addresses, and I don't think thats possible at all with Regex, but it'll at least minimise human-error
I also have customers request this frequently as well. It is really just to keep customers from doing something stupid like entering their phone number in the email field.
Just wanted to check for an empty String. ^$ worked
1

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

Comments

1

^$ 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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.