Skip to the content

Email Regex

A regular expression for e-mail addresses, ready to test: the pattern is filled in, with lines that should match and lines that should not.

Try:

JavaScript syntax, run by your browser's own RegExp engine.

6 matches.

Matches

  1. 1ada@example.comindex 0
  2. 2grace.hopper+news@mail.example.orgindex 16
  3. 3first_last@sub.domain.co.ukindex 51
  4. 4x@y.ioindex 79
  5. 5a..b@example.comindex 244
  6. 6ada@-example-.comindex 261
Cheat sheet

Characters

.
Any character except a line break
\d \D
A digit 0-9, anything but a digit
\w \W
A letter a-z, a digit or an underscore; anything else
\s \S
White space, anything but white space
[abc] [^abc]
One of a, b, c; any character except those
[a-z0-9]
A character in one of the ranges
\p{L}
A letter in any script (needs the u flag)
\. \\ \/
A literal dot, backslash, slash

How many

a*
Zero or more
a+
One or more
a?
Zero or one
a{3}
Exactly three
a{2,5}
Two to five
a{2,}
Two or more
a*? a+? a??
The same, but as few as possible (lazy)

Positions

^
Start of the text, or of a line with the m flag
$
End of the text, or of a line with the m flag
\b
The edge of a word
\B
Anywhere that is not the edge of a word

Groups

(abc)
A group whose text is remembered
(?<name>abc)
The same, with a name
(?:abc)
A group that is not remembered
a|b
Either a or b
\1 \k<name>
The same text a group matched earlier

Looking around

a(?=b)
a, only when b follows
a(?!b)
a, only when b does not follow
(?<=b)a
a, only when b comes before it
(?<!b)a
a, only when b does not come before it

In the replacement

$1 $2
The text of group 1, group 2
$<name>
The text of a named group
$&
The whole match
$` $'
The text before the match, after the match
$$
A dollar sign
Share this tool:TwitterFacebookLinkedInWhatsAppEmail

The pattern, piece by piece

For the addresses people normally have: a name, an @ and a domain with an ending. Each line is tested as a whole.

/^[\w.%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}$/gm
^The start of the line.
[\w.%+-]+The name in front of the @: one or more letters, digits, underscores, dots, percent signs, plus signs or hyphens.
@The at sign.
[A-Za-z0-9-]+The first part of the domain: letters, digits and hyphens.
(?:\.[A-Za-z0-9-]+)*Any number of further parts, each with a dot in front, as in mail.example.
\.[A-Za-z]{2,}A dot and the ending, such as .com or .museum: two letters or more.
$The end of the line.

What it gets right

These are the first lines in the box above. With the g and m flags every line is tested on its own, from its first character to its last.

ada@example.comMatch
grace.hopper+news@mail.example.orgMatch
first_last@sub.domain.co.ukMatch
x@y.ioMatch
plainaddressNo matchNo @.
@example.comNo matchNothing in front of the @.
ada@exampleNo matchNo ending behind the domain.
ada@example..comNo matchTwo dots in a row in the domain.
ada lovelace@example.comNo matchA space in the name.

Where it goes wrong

The last lines in the box are there to show the limits: text the pattern refuses although it is fine, or lets through although it is not.

o'brien@example.comNo matchAn apostrophe is allowed in the name, and real people have one.
"ada lovelace"@example.comNo matchInside quotes, a name may even contain a space.
josé@example.comNo matchLetters outside ASCII are allowed in addresses these days.
ada@localhostNo matchFine on a local network, where a host needs no dot.
a..b@example.comMatchTwo dots in a row are not allowed in the name.
ada@-example-.comMatchA part of a domain cannot start or end with a hyphen.

Before you rely on it

No pattern can tell whether an address exists, or whether the person who typed it can read its mail. The only real test is to send a message with a link in it. In a sign-up form, use a pattern to catch slips such as a missing @, and be generous about the rest.

The check that browsers do themselves on <input type="email"> differs a little. It lets ada@localhost and o'brien@example.com through, and like this pattern it refuses the quoted name and the accented letter.

To pull addresses out of a longer text instead of testing whole lines, take off the ^ and the $.

The regex tester page explains the flags, has a cheat sheet of the syntax, and tells what happens when a pattern takes too long.

See all 12