Most email regexes on the internet fail in one of two ways. They turn away real people (jane+news@, o'brien@), or they let obvious junk through (jane@@example.com). Some manage both.
This is the pattern our own verifier runs before anything else, the 19 addresses we tested it on next to two popular alternatives, and one experiment that surprised us: a regex from countless tutorials that took 49 seconds to answer for a single 74-character string.
The pattern
const EMAIL =
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/;
function looksLikeEmail(value) {
const email = value.trim();
return email.length <= 254 && EMAIL.test(email) && email.split('@')[0].length <= 64;
}
It is the rule the HTML standard uses for <input type="email">, with one change: we require at least one dot after the @. A browser accepts jane@localhost; a list of customers has no use for it.
Read from left to right:
[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+: the part before the@. Letters, digits, dots and the symbols the standard allows, including+and'.@: exactly one, since the@itself is not in either character set.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?: one domain label. Up to 63 characters, letters, digits and hyphens, never starting or ending with a hyphen.(?:\.label)+: one or more further labels, each after a dot. That is whereexample.com,mail.example.co.ukandexample.photographyall fit.
The two length checks come from the mail standards: 64 characters at most before the @, 254 in total. Regexes are bad at counting, so we count in code.
Tested on 19 addresses
We ran three patterns against the same list:
- Simple:
/^\S+@\S+\.\S+$/, the one-liner you find in many answers. - Tutorial:
/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, still copied into a lot of forms. - Ours, above.
| Address | Simple | Tutorial | Ours | Really valid? |
|---|---|---|---|---|
jane.doe@example.com |
yes | yes | yes | yes |
jane.doe+news@example.com |
yes | no | yes | yes |
o'brien@example.ie |
yes | no | yes | yes |
JANE@EXAMPLE.COM |
yes | yes | yes | yes |
jane@mail.example.photography |
yes | yes | yes | yes |
jane@example |
no | no | no | no |
jane doe@example.com |
no | no | no | no |
jane@@example.com |
yes | no | no | no |
jane@-example.com |
yes | yes | no | no |
jane@example..com |
yes | no | no | no |
jane@example.com. |
yes | no | no | no |
jane@example.c |
yes | no | yes | format yes, but no such domain |
jane..doe@example.com |
yes | yes | yes | no, without quotes |
.jane@example.com |
yes | yes | yes | no, without quotes |
"jane doe"@example.com |
no | no | no | yes, but almost never used |
josé@example.com |
yes | no | no | yes, on servers that support it |
jane@例子.广告 |
yes | no | no | yes, on servers that support it |
jane@123.123.123.123 |
yes | yes | yes | format yes, but .123 is not a real domain |
jane@localhost |
no | no | no | not on the internet |
What we take from it:
- The tutorial pattern turns away real customers. A
+or an apostrophe is enough. Every one of those is a person who could not sign up. - The simple pattern lets almost anything through. Fine for a quick hint, not as the only check.
- Ours has known gaps, on purpose. It accepts dots in odd places (
jane..doe@), because the HTML standard does and because the mailbox check that follows catches them anyway. It refuses quoted local parts and non-ASCII addresses, which are valid but rare enough that we let a person retype rather than let a malformed address in. If your users write in scripts other than Latin, test that decision against your own sign-ups.
If you want the dot rules too (no dot at the start or end of the local part, never two in a row), add three lookarounds and use this in looksLikeEmail instead. They cost nothing measurable:
const EMAIL_STRICT =
/^(?!\.)(?!.*\.\.)[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+(?<!\.)@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/;
The regex that took 49 seconds
The tutorial pattern has a second problem, and it is worse than turning away a few addresses. Look at its end: ([a-zA-Z0-9]{2,4})+$. A group that matches 2 to 4 letters, repeated. When the string almost matches and then fails at the last character, the engine tries every way of cutting the letters into pieces of 2, 3 and 4 before giving up. The number of ways grows exponentially with the length.
We timed it in Node.js 22 with strings like jane@example.aaaa…aaa!:
| Length | Tutorial pattern | Ours |
|---|---|---|
| 44 characters | 4.9 ms | 0.2 ms |
| 54 characters | 25 ms | 0.07 ms |
| 64 characters | 1.1 seconds | 0.01 ms |
| 74 characters | 49 seconds | 0.01 ms |
Ten more characters, fifty times longer. In a browser, that freezes the tab. On a server, one request like this can keep a process busy while every other request waits. It even has a name: ReDoS, regular expression denial of service.
Two habits keep you safe:
- No repeated group inside a repeated group when both can match the same characters (
(a+)+,(\w{2,4})+). In our pattern every repeated label starts with a dot, so there is only one way to cut a domain into labels. - Cut the length first. Nothing longer than 254 characters is an email address, so refuse it before the regex runs.
The same check in Python and PHP
Python, with one trap that catches a lot of people:
import re
EMAIL = re.compile(
r"[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?"
r"(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+"
)
def looks_like_email(value: str) -> bool:
email = value.strip()
if len(email) > 254 or not EMAIL.fullmatch(email):
return False
return len(email.split("@")[0]) <= 64
Use fullmatch. With re.match and a $ at the end, Python also accepts the address followed by a newline, because $ matches just before a final \n.
PHP ships a check of its own:
$ok = filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
On our list it behaved much like the strict version above: it refused leading and double dots, quoted local parts and non-ASCII addresses, and accepted jane@example.c. For addresses like josé@example.com, PHP has the FILTER_FLAG_EMAIL_UNICODE flag.
In the browser, <input type="email" required> gives you the standard rule for free, with a message in the visitor's language. It runs only on their machine, so check again on the server.
What no regex can tell you
anything@gmail.com passes every pattern above. So does jane@gmial.com. A regex checks the shape of the text, not the world behind it. After the pattern, our verifier:
- Looks up the domain's MX records: does
example.comreceive email at all? You can try it with our MX lookup. - Asks the mail server whether the mailbox exists, the way a server would before delivering, then hangs up without sending anything.
- Tests whether the domain accepts every address with a made-up one. Such catch-all domains can't confirm a single mailbox.
- Flags disposable and role addresses (
@mailinator.com,info@) and suggests fixes for common typos likegmial.com.
Steps 2 and 3 are the ones you can't do from a browser: they need a server that talks SMTP, with a sending reputation good enough that mail servers answer it honestly. The details, with code, are in how to check if an email address is valid.
If you would rather not run that part yourself, our email verification API does it in one request per address, and the free email checker shows the result for one address, without an account.
Frequently asked questions
Is there a regex that follows RFC 5322 exactly?
Yes, and you don't want it. The Perl module Mail::RFC822::Address carries one of about 6,000 characters, for the older RFC 822. It accepts comments, quoted strings and folded whitespace that no sign-up form needs, and it still can't tell you whether the mailbox exists.
Why does my regex reject .photography or .museum addresses?
Probably a top-level domain limited to 2 to 4 letters, like [a-z]{2,4}$. Today there are hundreds of longer ones. Allow up to 63 characters per label, as in our pattern.
Should I lowercase email addresses?
Store them as typed and compare them in lowercase. The domain part is never case-sensitive. The part before the @ technically can be, but in practice providers treat Jane and jane as the same mailbox.
Is type="email" enough?
For instant feedback, yes. For anything you store, no: anyone can send a request to your server without going through the form.
Sources
- HTML Standard: E-mail state (
type=email), including the note that its rule is a "willful violation" of RFC 5322 - RFC 5321, section 4.5.3.1: size limits (64 octets before the
@) and RFC 3696 erratum 1690 (254 characters in total) - RFC 5322, section 3.4.1: addr-spec
- RFC 6531: SMTP extension for internationalized email
- OWASP: Regular expression Denial of Service (ReDoS)
- PHP manual: validate filters