Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | Start of string |
\+ |
Quantified literal '+': matches the character '+' literally, 0 or 1 time (due to '?') | + |
? |
Quantifier: 0 or 1 occurrence of the preceding token (\+) | Empty or '+' |
[0-9\s\-\(\)] |
Character class: matches any single digit (0-9), whitespace (\s), hyphen (-), or parentheses (() | 0, 1, space, -, (, ) |
{10,15} |
Quantifier: matches the preceding token (the character class) between 10 and 15 times | 10 to 15 characters from the class |
$ |
Anchor: asserts position at end of the string | End of string |
Matches
+1234567890123 456 7890(123)456-7890123-456-78901234567890++123456789012345+123 456 7890
Does not match
+12345678912345678901+1234567890123456abc1234567890123-456-78912345678901234567
Common pitfalls
- Overlooking that the pattern allows any combination of digits, spaces, hyphens, and parentheses, not just phone numbers.
- Assuming the pattern enforces a specific phone number format (e.g., E.164).
- Forgetting that the '+' is optional, leading to false negatives if it is present.
- Not accounting for whitespace in the character class, which may cause unintended matches.
Performance notes
Low risk of catastrophic backtracking due to fixed quantifiers and no nested quantifiers. The character class is simple, so performance is generally good. Ensure anchors (^ and $) are used to prevent partial matches.