Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | Start of string |
\+? |
Optional literal '+' character (quantifier '?' means 0 or 1) | + |
[0-9] |
Single digit (0-9) | 1 |
\s? |
Optional whitespace character (quantifier '?' means 0 or 1) | |
\(? |
Optional literal '(' character (quantifier '?' means 0 or 1) | ( |
\d{3} |
Exactly 3 digits (0-9) | 123 |
\)? |
Optional literal ')' character (quantifier '?' means 0 or 1) | ) |
[-.\s]? |
Optional separator: hyphen, dot, or whitespace (quantifier '?' means 0 or 1) | - |
\d{3} |
Exactly 3 digits (0-9) | 456 |
[-.\s]? |
Optional separator: hyphen, dot, or whitespace (quantifier '?' means 0 or 1) | . |
\d{4} |
Exactly 4 digits (0-9) | 7890 |
$ |
Anchor: asserts position at end of the string | End of string |
Matches
1234567890+1 123 456 78901 (123) 456-78901.123.456.7890123-456-7890+12345678901 123 4567890
Does not match
12345678912345678901123-456-789123.456.789011234 567 890abc1234567890123)456-7890(123)456-7890
Common pitfalls
- Does not validate country codes beyond '+' (e.g., '+44' would fail after the first digit).
- Allows invalid separators in the middle (e.g., '123..456.7890').
- Does not enforce minimum/maximum length for the entire number.
- Allows leading/trailing whitespace if not anchored properly (though ^ and $ prevent this).
- Does not distinguish between domestic and international formats clearly.
Performance notes
Low risk of catastrophic backtracking due to simple quantifiers and no nested quantifiers. Anchors (^ and $) ensure efficient matching. However, the pattern is permissive and may match unintended formats.