Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches a North American phone number in various formats, including optional country code (+1), area code, and exchange code, with strict digit constraints to avoid invalid numbers.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of '1234567890'
(?: Start of non-capturing group N/A
\+? Optional literal '+' character (0 or 1 times) '' or '+'
1 Literal digit '1' 1
)? End of non-capturing group, quantifier: 0 or 1 times '' or '+1'
[2-9] Character class: any digit from 2 to 9 2, 3, ..., 9
\d{2} Digit class (0-9) repeated exactly 2 times 00, 12, ..., 99
[2-9] Character class: any digit from 2 to 9 2, 3, ..., 9
\d{2} Digit class (0-9) repeated exactly 2 times 00, 12, ..., 99
\d{4} Digit class (0-9) repeated exactly 4 times 0000, 1234, ..., 9999
$ Anchor: asserts position at end of the string End of '1234567890'

Matches

  • 2125551234
  • +12125551234
  • 12125551234
  • 212-555-1234
  • (212)5551234
  • 212.555.1234
  • 212 555 1234

Does not match

  • 1234567890
  • +11234567890
  • 02125551234
  • 212555123
  • 21255512345
  • 1-800-FLOWERS

Common pitfalls

  • Forgetting that the first digit of area code and exchange code cannot be 0 or 1
  • Assuming the pattern matches international numbers (only +1 is supported)
  • Overlooking that the country code (+1) is optional but must be followed by a valid 10-digit number
  • Not accounting for non-digit separators (e.g., hyphens, spaces) in the input string

Performance notes

The pattern is linear in complexity (O(n)) and does not pose catastrophic backtracking risks. Anchors (^ and $) ensure efficient matching by limiting the search scope. For large inputs, ensure the string is trimmed to avoid unnecessary checks.

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 match all valid North American phone numbers?

No. It matches numbers with a 3-digit area code and 3-digit exchange code where neither starts with 0 or 1, followed by 4 digits. It does not account for all special cases (e.g., toll-free numbers like 800).

Can this pattern match phone numbers with extensions?

No. The pattern strictly matches 10 digits (or 11 with +1) and does not account for extensions (e.g., 'x1234').

Why is the country code (+1) optional?

The pattern is designed to match both domestic (10-digit) and international (11-digit with +1) North American numbers, making the country code optional.

Does this pattern work with parentheses or other separators?

No. The pattern as written does not explicitly handle separators like parentheses or hyphens. Input strings must be normalized (e.g., '2125551234' or '+12125551234') for a match.