Regex patterns/Validation
Email address
A pragmatic email check — the one worth using, not the one that is technically complete.
/^[\w.+-]+@[\w-]+\.[\w.-]+$/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
Follow the line from left to right — every path you can trace is a string this pattern matches.
^[\w.+-]+@[\w-]+\.[\w…In order:^The start of the text (or of a line with the m flag)[\w.+-]+Any one of: a word character (letter, digit or underscore), ".", "+" or "-", one or more times, as many as possible@The character "@"[\w-]+Any one of: a word character (letter, digit or underscore) or "-", one or more times, as many as possible\.The character "."[\w.-]+Any one of: a word character (letter, digit or underscore), "." or "-", one or more times, as many as possible$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.