Regex patterns/Validation

Email address

A pragmatic email check — the one worth using, not the one that is technically complete.

/^[\w.+-]+@[\w-]+\.[\w.-]+$/
Open it in Rex

The problem

Check that something looks like an email address before you try to send to it: some characters, an @, a domain with at least one dot.

How it reads

start[\w.+-]repeat@[\w-]repeat\.[\w.-]repeatend

Follow the line from left to right — every path you can trace is a string this pattern matches.

  1. ^[\w.+-]+@[\w-]+\.[\w…In order:
  2. ^The start of the text (or of a line with the m flag)
  3. [\w.+-]+Any one of: a word character (letter, digit or underscore), ".", "+" or "-", one or more times, as many as possible
  4. @The character "@"
  5. [\w-]+Any one of: a word character (letter, digit or underscore) or "-", one or more times, as many as possible
  6. \.The character "."
  7. [\w.-]+Any one of: a word character (letter, digit or underscore), "." or "-", one or more times, as many as possible
  8. $The end of the text (or of a line with the m flag)

Matches

  • ada@example.com
  • first.last+tag@sub.example.co.uk
  • a@b.co

Does not match

  • not-an-email
  • missing@domain
  • @example.com
  • two@@example.com
  • spaces in@example.com

Where it bites

  • This deliberately does not implement RFC 5322. The complete grammar allows quoted strings, comments and nested parentheses; the regex for it is thousands of characters long and still cannot tell you whether the mailbox exists.
  • It accepts addresses no mail server would take, such as a@b.c — that is the correct trade-off for a client-side hint.
  • The only real validation is sending a confirmation email. Treat this as a typo-catcher.

More validation patterns