Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^(?:\(?1[-. ]?)?([0-9]{3})[-. ]?([0-9]{3})[-. ]?([0-9]{4})$

Matches North American phone numbers in various formats, including optional country code (+1), area code, and separators like spaces, dots, or hyphens.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string
(?: Start of a non-capturing group Start of optional country code block
\+? Optional literal plus sign (country code prefix) + or nothing
1 Literal digit 1 (country code for US/Canada) 1
[-. ]? Optional separator: hyphen, dot, or space - or . or space or nothing
)? End of non-capturing group, optional (0 or 1 times) Entire country code block may be absent
([0-9]{3}) Capturing group for area code: exactly 3 digits 123
[-. ]? Optional separator: hyphen, dot, or space - or . or space or nothing
([0-9]{3}) Capturing group for central office code: exactly 3 digits 456
[-. ]? Optional separator: hyphen, dot, or space - or . or space or nothing
([0-9]{4}) Capturing group for line number: exactly 4 digits 7890
$ Anchor: asserts position at end of the string End of string

Matches

  • 1234567890
  • +1 123 456 7890
  • 1-123-456-7890
  • 123.456.7890
  • +11234567890
  • 1234567890
  • 1 123 456 7890
  • 123-456-7890

Does not match

  • 123456789
  • 12345678901
  • 123-456-789
  • abc1234567890
  • +44 123 456 7890
  • 123 456 78901

Common pitfalls

  • Missing anchors (^ and $) may cause partial matches within longer strings.
  • The pattern does not validate area codes or exchange codes (e.g., 000 is technically allowed).
  • Overly permissive separators may match unintended characters if not strictly controlled.
  • The pattern does not account for international numbers outside the +1 country code.

Performance notes

The pattern is linear in complexity with no nested quantifiers, so catastrophic backtracking is not a concern. However, using anchors (^ and $) is critical to prevent partial matches in longer strings. The optional non-capturing group for the country code is efficient as it is evaluated only once.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Does this pattern validate real phone numbers?

No, it only checks the format. It does not verify if the area code or exchange code is valid or in use.

Can this pattern match international numbers?

No, it is designed specifically for North American numbers with the +1 country code.

Why are the separators optional and flexible?

To accommodate common formatting variations like spaces, hyphens, or dots between number groups.