Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | Start of string |
(?: |
Start of non-capturing group | Start of optional country code and separator group |
+? |
Optional plus sign (quantifier: 0 or 1) | + or nothing |
1 |
Literal digit '1' (country code for North America) | 1 |
[-.\s]? |
Optional separator (dot, hyphen, or whitespace) | . |
)? |
End of non-capturing group, quantifier: 0 or 1 (optional) | Entire country code group optional |
\(? |
Optional opening parenthesis | ( |
([0-9]{3}) |
Capturing group: exactly 3 digits (area code) | 123 |
\)? |
Optional closing parenthesis | ) |
[-.\s]? |
Optional separator (dot, hyphen, or whitespace) | . |
([0-9]{3}) |
Capturing group: exactly 3 digits (exchange code) | 456 |
[-.\s]? |
Optional separator (dot, hyphen, or whitespace) | . |
([0-9]{4}) |
Capturing group: exactly 4 digits (subscriber number) | 7890 |
$ |
Anchor: asserts position at end of the string | End of string |
Matches
1234567890+1 123 456 7890(123) 456-7890123.456.7890+1(123)45678901-123-456-789012345678901 123 456 7890
Does not match
12345123-456-78912345678901abc1234567123)456-7890123.456.789+44 123 456 7890
Common pitfalls
- Fails to match numbers with more than 10 digits (e.g., extensions).
- Does not validate area codes (e.g., 000 is matched but invalid).
- Allows separators between digits but does not enforce consistency (e.g., 123.456-7890 is valid).
- Does not handle international numbers with different country codes.
- May match invalid formats like '123)456-7890' due to optional parentheses.
Performance notes
The pattern is linear in complexity with no nested quantifiers, so catastrophic backtracking is not a concern. However, the many optional groups and separators may lead to many possible paths during matching, which could impact performance on very long strings. Anchoring at start and end ensures no partial matches.