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), optional parentheses around area code, and optional separators (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 (for optional country code +1 with optional prefix '+' and optional separator) Optional country code part
\+? Optional literal '+' character (0 or 1 times) + or nothing
1 Literal digit '1' (country code for North America) 1
[-. ]? Optional separator: one of '-', '.', or space (0 or 1 times) -, ., space, or nothing
)? End of non-capturing group, makes the entire group optional (0 or 1 times) Optional country code block
\(? Optional literal '(' character (0 or 1 times) (
([0-9]{3}) Capturing group: exactly 3 digits (area code) 123
\)? Optional literal ')' character (0 or 1 times) )
[-. ]? Optional separator: one of '-', '.', or space (0 or 1 times) -, ., space, or nothing
([0-9]{3}) Capturing group: exactly 3 digits (exchange code) 456
[-. ]? Optional separator: one of '-', '.', or space (0 or 1 times) -, ., space, or nothing
([0-9]{4}) Capturing group: exactly 4 digits (subscriber number) 7890
$ Anchor: asserts position at end of the string End of string

Matches

  • 1234567890
  • +1 123 456 7890
  • (123)4567890
  • 123.456.7890
  • 1-123-456-7890
  • +1(123)456-7890
  • 1234567890
  • 1 123 456 7890

Does not match

  • 12345
  • 123456789
  • 123-456-789
  • 123.456.789
  • 12345678901
  • abc1234567
  • +44 123 456 7890
  • 123)456-7890

Common pitfalls

  • Fails to match numbers with more than 10 digits (e.g., extensions).
  • Does not validate area codes (e.g., 000, 111) or exchange codes (e.g., 000).
  • May incorrectly match invalid separators if not careful (e.g., '123_456_7890').
  • Overly permissive with optional separators, allowing ambiguous formats like '1234567890' or '123.4567890'.

Performance notes

Low risk of catastrophic backtracking due to simple quantifiers and limited alternation. Anchors (^ and $) ensure full-string matching, preventing partial matches. Non-capturing groups improve efficiency.

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 validate area codes, exchange codes, or country codes against real-world data.

Why use non-capturing groups (?:...)?

Non-capturing groups improve performance by avoiding unnecessary capture overhead for groups that are not referenced later.

Can this pattern match international numbers?

No, it is designed specifically for North American numbers (country code +1) and will not match other international formats.

What happens if the country code is present but the '+' is missing?

The pattern still matches as long as the '1' is present (e.g., '1 123 456 7890' is valid). The '+' is optional.