- Home
- Tools
- Developer Tools
- Regex Tester
- Phone Number Regex
Phone Number Regex
A regular expression for phone numbers in international E.164 notation, with a second pattern for numbers written the American way.
JavaScript syntax, run by your browser's own RegExp engine.
5 matches.
Matches
- 1
+14155550132index 0 - 2
+442079460958index 13 - 3
+447700900123index 27 - 4
+61255501234index 41 - 5
+12index 141
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
The pattern, piece by piece
For numbers in E.164 notation, the international form that phone networks use: a plus sign, the country code and the number, without spaces.
/^\+[1-9]\d{1,14}$/gm| Piece | What it does |
|---|---|
^ | The start of the line. |
\+ | A plus sign. It needs the backslash, because a bare + means one or more of what stands before it. |
[1-9] | The first digit of the country code, which is never 0. |
\d{1,14} | One to fourteen more digits. With the first one that makes fifteen at most, the longest number the standard allows. |
$ | 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.
| Line | Result | Why |
|---|---|---|
+14155550132 | Match | |
+442079460958 | Match | |
+447700900123 | Match | |
+61255501234 | Match | |
14155550132 | No match | No plus sign. |
0014155550132 | No match | The dialling prefix 00 where the plus should be. |
+0155550132 | No match | A country code cannot start with 0. |
+1234567890123456 | No match | Sixteen digits: one too many. |
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.
| Line | Result | What is wrong with that |
|---|---|---|
+1 415 555 0132 | No match | The first number again, with spaces as people write it. |
(415) 555-0132 | No match | And in its national form. The second pattern below is for that. |
+12 | Match | Two digits are no phone number. The standard sets a longest length, not a shortest. |
Before you rely on it
Phone formats differ from country to country, so there is no single pattern for numbers as people write them. E.164 is the way around that: let people type their number as they are used to, strip the spaces, hyphens, dots and brackets, and test and store the result in this form.
What no pattern knows is whether a country code exists, how long numbers are in that country, or whether a number is in use. Google's libphonenumber library has the rules per country.
A pattern for numbers written the American way
For the United States and Canada: ten digits, in which the area code and the three digits after it both start with 2 to 9. Brackets around the area code are optional, and so is a space, hyphen or dot between the groups. It does not check that an opening bracket is closed again.
/^\(?[2-9]\d{2}\)?[ .-]?[2-9]\d{2}[ .-]?\d{4}$/gmRun on this text:
(415) 555-0132 415-555-0132 415.555.0132 4155550132 555-0132 (115) 555-0132 415-155-0132 +1 415 555 0132
it finds (415) 555-0132, 415-555-0132, 415.555.0132 and 4155550132, and nothing else.
The regex tester page explains the flags, has a cheat sheet of the syntax, and tells what happens when a pattern takes too long.