breakitdown

Regex for a US phone number

Answer

^(?:\+?1[-. ]?)?\(?([2-9]\d{2})\)?[-. ]?([2-9]\d{2})[-. ]?(\d{4})$ accepts 10-digit US numbers written as 555-867-5309, (555) 867-5309, 555.867.5309 or +1 555 867 5309, and captures the area code, exchange and line number.

flags: g all matches · i ignore case · m ^ $ per line · s . matches newlines · u Unicode

JavaScript ✓PCRE ✓

No catastrophic-backtracking patterns found. (A heuristic check: nested and adjacent repeats, overlapping alternatives.)

  1. ^Start of the stringanchor
  2. (?:…)Non-capturing group: groups without rememberinggroup
  3. \+The character '+' (escaped, so it is literal)literal
  4. ?Repeat it optionally (zero or one time)quantifier
  5. 1The character '1' literallyliteral
  6. [-. ]One character: '-', '.' or a spacecharacter class
  7. ?Repeat the class optionally (zero or one time)quantifier
  8. ?Then repeat the whole group optionally (zero or one time)quantifier
  9. \(The character '(' (escaped, so it is literal)literal
  10. ?Repeat it optionally (zero or one time)quantifier
  11. (…)Capturing group #1: remembers what it matchedgroup
  12. [2-9]One character: '2' to '9'character class
  13. \dA digit (0-9)character type
  14. {2}Repeat it exactly 2 timesquantifier
  15. \)The character ')' (escaped, so it is literal)literal
  16. ?Repeat it optionally (zero or one time)quantifier
  17. [-. ]One character: '-', '.' or a spacecharacter class
  18. ?Repeat the class optionally (zero or one time)quantifier
  19. (…)Capturing group #2: remembers what it matchedgroup
  20. [2-9]One character: '2' to '9'character class
  21. \dA digit (0-9)character type
  22. {2}Repeat it exactly 2 timesquantifier
  23. [-. ]One character: '-', '.' or a spacecharacter class
  24. ?Repeat the class optionally (zero or one time)quantifier
  25. (…)Capturing group #3: remembers what it matchedgroup
  26. \dA digit (0-9)character type
  27. {4}Repeat it exactly 4 timesquantifier
  28. $End of the stringanchor

3 capturing groups.

Examples

Checked at build time in JavaScript and in Python's re.

InputMatches?
555-867-5309✓ match
(555) 867-5309✓ match
+1 555 867 5309✓ match
5558675309✓ match
155-867-5309✗ no match
555-067-5309✗ no match
867-5309✗ no match
555-867-5309 x12✗ no match
(555-867-5309✓ match
+44 20 7946 0958✗ no match

Try your own text

One string per line: each line is tested on its own (the m flag is added here).

How it works

Under the North American Numbering Plan, area codes and exchange codes both start with 2-9. A leading 0 or 1 is never valid there, because those digits were used for operator and long-distance dialing. That is why the pattern uses [2-9]\d{2} instead of \d{3}.

The best practice is to strip everything except digits first, then check the result. s.replace(/\D/g, "") turns every format into a plain digit string, which you can test with ^1?[2-9]\d{2}[2-9]\d{6}$ and store in one canonical form (E.164: +15558675309). Users then type the number however they like.

Variants

Gotchas

Related explainers

← Explain any regular expression