Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches international or domestic phone numbers with optional country code (+), optional parentheses around area code, and flexible separators (space, dot, or hyphen).

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string
\+? Optional literal '+' character (quantifier '?' means 0 or 1) +
[0-9] Single digit (0-9) 1
\s? Optional whitespace character (quantifier '?' means 0 or 1)
\(? Optional literal '(' character (quantifier '?' means 0 or 1) (
\d{3} Exactly 3 digits (0-9) 123
\)? Optional literal ')' character (quantifier '?' means 0 or 1) )
[-.\s]? Optional separator: hyphen, dot, or whitespace (quantifier '?' means 0 or 1) -
\d{3} Exactly 3 digits (0-9) 456
[-.\s]? Optional separator: hyphen, dot, or whitespace (quantifier '?' means 0 or 1) .
\d{4} Exactly 4 digits (0-9) 7890
$ Anchor: asserts position at end of the string End of string

Matches

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

Does not match

  • 123456789
  • 12345678901
  • 123-456-789
  • 123.456.78901
  • 1234 567 890
  • abc1234567890
  • 123)456-7890
  • (123)456-7890

Common pitfalls

  • Does not validate country codes beyond '+' (e.g., '+44' would fail after the first digit).
  • Allows invalid separators in the middle (e.g., '123..456.7890').
  • Does not enforce minimum/maximum length for the entire number.
  • Allows leading/trailing whitespace if not anchored properly (though ^ and $ prevent this).
  • Does not distinguish between domestic and international formats clearly.

Performance notes

Low risk of catastrophic backtracking due to simple quantifiers and no nested quantifiers. Anchors (^ and $) ensure efficient matching. However, the pattern is permissive and may match unintended formats.

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 for a specific format with digits and optional separators. It does not validate country codes, area codes, or number ranges.

Why does it allow '1' as the first digit?

The pattern starts with an optional '+' followed by a single digit (0-9). This allows for a country code '+' or a domestic number starting with '1'.

Can this pattern match numbers with extensions (e.g., '123-456-7890 x1234')?

No, the pattern ends with exactly 4 digits and does not account for extensions or additional characters.