Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^(\+?\d{1,3}[- ]?)?\(?\d{3}\)?[- ]?\d{3}[- ]?\d{4}$

This regex matches international and domestic phone numbers with optional country codes, parentheses around area codes, and various separators including spaces or hyphens.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the position at the start of the string Start of input
( Start of a non-capturing group (optional country code block)
\+? Optional plus sign (quantifier ? makes it optional) + or nothing
\d{1,3} 1 to 3 digits (country code) 1, 44, 999
[- ]? Optional separator: either a hyphen or space (quantifier ? makes it optional) - or space or nothing
)? End of the non-capturing group and makes the entire group optional
\(? Optional opening parenthesis (quantifier ? makes it optional) (
\d{3} Exactly 3 digits (area code) 123
\)? Optional closing parenthesis (quantifier ? makes it optional) )
[- ]? Optional separator: either a hyphen or space (quantifier ? makes it optional) - or space or nothing
\d{3} Exactly 3 digits (exchange code) 456
[- ]? Optional separator: either a hyphen or space (quantifier ? makes it optional) - or space or nothing
\d{4} Exactly 4 digits (subscriber number) 7890
$ Anchor: asserts the position at the end of the string End of input

Matches

  • +1 (123) 456-7890
  • 44 123 456 7890
  • 123-456-7890
  • (123)4567890
  • 1234567890
  • +999 123 456 7890
  • 1-800-555-1234

Does not match

  • +123456789012
  • 123
  • 12345
  • 123-456
  • 123-456-789
  • 123.456.7890
  • abc1234567890
  • 123)456-7890

Common pitfalls

  • Overly permissive with separators leading to ambiguous matches
  • Does not validate country codes or area codes for correctness
  • May match invalid sequences like '123)456-7890' due to optional parentheses
  • No strict validation of digit groupings (e.g., area code '000' is allowed but invalid in real phone numbers

Performance notes

The pattern is generally efficient due to limited quantifiers and alternations. However, the optional groups and quantifiers could lead to backtracking if the input is malformed. Anchoring at both ends ensures no partial matches. Consider simplifying if strict validation is required.

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 regex validate real phone numbers?

No, it only matches common formatting patterns. It does not validate country codes, area codes, or subscriber numbers for correctness.

Can this regex handle extensions like 'x1234'?

No, the regex does not account for phone number extensions.

Why are parentheses optional?

Parentheses around area codes are common in some regions but not required in others, so the regex allows both formats.

Is this regex suitable for production use?

Not without additional validation. It is permissive and may match invalid sequences. Consider using a dedicated phone number library for production.