Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | Start of string |
(?: |
Start of a non-capturing group | Start of optional country code block |
\+? |
Optional literal plus sign (country code prefix) | + or nothing |
1 |
Literal digit 1 (country code for US/Canada) | 1 |
[-. ]? |
Optional separator: hyphen, dot, or space | - or . or space or nothing |
)? |
End of non-capturing group, optional (0 or 1 times) | Entire country code block may be absent |
([0-9]{3}) |
Capturing group for area code: exactly 3 digits | 123 |
[-. ]? |
Optional separator: hyphen, dot, or space | - or . or space or nothing |
([0-9]{3}) |
Capturing group for central office code: exactly 3 digits | 456 |
[-. ]? |
Optional separator: hyphen, dot, or space | - or . or space or nothing |
([0-9]{4}) |
Capturing group for line number: exactly 4 digits | 7890 |
$ |
Anchor: asserts position at end of the string | End of string |
Matches
1234567890+1 123 456 78901-123-456-7890123.456.7890+1123456789012345678901 123 456 7890123-456-7890
Does not match
12345678912345678901123-456-789abc1234567890+44 123 456 7890123 456 78901
Common pitfalls
- Missing anchors (^ and $) may cause partial matches within longer strings.
- The pattern does not validate area codes or exchange codes (e.g., 000 is technically allowed).
- Overly permissive separators may match unintended characters if not strictly controlled.
- The pattern does not account for international numbers outside the +1 country code.
Performance notes
The pattern is linear in complexity with no nested quantifiers, so catastrophic backtracking is not a concern. However, using anchors (^ and $) is critical to prevent partial matches in longer strings. The optional non-capturing group for the country code is efficient as it is evaluated only once.