Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
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+1212555123412125551234212-555-1234(212)5551234212.555.1234212 555 1234
Does not match
1234567890+1123456789002125551234212555123212555123451-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.