Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

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

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string
(?: Start of non-capturing group Start of optional country code and separator group
+? Optional plus sign (quantifier: 0 or 1) + or nothing
1 Literal digit '1' (country code for North America) 1
[-.\s]? Optional separator (dot, hyphen, or whitespace) .
)? End of non-capturing group, quantifier: 0 or 1 (optional) Entire country code group optional
\(? Optional opening parenthesis (
([0-9]{3}) Capturing group: exactly 3 digits (area code) 123
\)? Optional closing parenthesis )
[-.\s]? Optional separator (dot, hyphen, or whitespace) .
([0-9]{3}) Capturing group: exactly 3 digits (exchange code) 456
[-.\s]? Optional separator (dot, hyphen, or whitespace) .
([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) 456-7890
  • 123.456.7890
  • +1(123)4567890
  • 1-123-456-7890
  • 1234567890
  • 1 123 456 7890

Does not match

  • 12345
  • 123-456-789
  • 12345678901
  • abc1234567
  • 123)456-7890
  • 123.456.789
  • +44 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 is matched but invalid).
  • Allows separators between digits but does not enforce consistency (e.g., 123.456-7890 is valid).
  • Does not handle international numbers with different country codes.
  • May match invalid formats like '123)456-7890' due to optional parentheses.

Performance notes

The pattern is linear in complexity with no nested quantifiers, so catastrophic backtracking is not a concern. However, the many optional groups and separators may lead to many possible paths during matching, which could impact performance on very long strings. Anchoring at start and end ensures no partial matches.

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

Why is the country code group optional?

To allow matching US/Canada numbers with or without the +1 country code.

Can this pattern match numbers with extensions?

No, the pattern strictly matches 10-digit numbers (3-digit area code + 3-digit exchange + 4-digit subscriber number).

Does this pattern validate area codes?

No, it matches any 3-digit sequence, even invalid ones like 000 or 911.

Why are separators optional and inconsistent?

To accommodate common formatting variations in phone numbers, but this may lead to ambiguous matches.