Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches international and local phone numbers with optional country codes, parentheses, and separators like spaces or hyphens. Supports flexible formatting common in global phone number standards.

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
\+ Literal plus sign for country code prefix +
\d{1,3} 1 to 3 digits for country code 1, 44, 998
[- ]? Optional hyphen or space separator -
)? End of non-capturing group, quantifier: 0 or 1 occurrence (makes country code optional) Optional country code block
\(? Optional opening parenthesis (
\d{1,4} 1 to 4 digits for area code or local prefix 1, 20, 1234
\)? Optional closing parenthesis )
[- ]? Optional hyphen or space separator -
\d{1,4} 1 to 4 digits for next segment of phone number 1, 20, 1234
[- ]? Optional hyphen or space separator -
\d{1,9} 1 to 9 digits for the remaining part of the phone number 1, 123456789
$ Anchor: asserts position at end of the string End of string

Matches

  • +1 (800) 555-1234
  • +44 20 7946 0958
  • 123456789
  • (123) 456-7890
  • +998-12-345-67-89
  • 1-800-555-1234
  • 123 456 7890
  • 1234567890

Does not match

  • +1234567890123456
  • 123-456-78901
  • +1 (800) 555-12345
  • abc123456789
  • (123)4567890
  • 123 456 789
  • ++1 123 456 7890

Common pitfalls

  • Overly permissive for valid phone numbers (e.g., accepts '1234567890123456' which is too long)
  • Does not validate country-specific phone number rules (e.g., length, valid prefixes)
  • May match strings with invalid separators (e.g., '123-456 7890')
  • Does not handle extensions or special characters like '.' or '/'
  • Allows leading/trailing separators if not anchored properly (though ^ and $ mitigate this)

Performance notes

The pattern is generally efficient due to limited quantifiers and no nested quantifiers. However, the optional groups and multiple digit ranges could lead to backtracking in pathological cases. Anchoring with ^ and $ ensures the regex engine does not scan the entire string unnecessarily.

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 checks the format (e.g., digits, separators, parentheses). It does not validate country codes, area codes, or actual phone number rules.

Can this regex match phone numbers with extensions?

No, extensions (e.g., 'x1234') are not supported in this pattern.

Why are there optional separators between digits?

To accommodate common phone number formatting styles, such as spaces, hyphens, or no separators at all.

Does this regex support international phone numbers with varying lengths?

Yes, it supports country codes of 1-3 digits and local numbers of varying lengths (1-4 digits for area code, 1-4 digits for next segment, and 1-9 digits for the remaining part).

Can this regex match phone numbers with parentheses around the area code?

Yes, the pattern explicitly allows optional parentheses around the area code (e.g., '(123) 456-7890').