Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | +1234567890 |
\+ |
Escaped plus sign: matches a literal '+' character | +1234567890 |
? |
Quantifier: matches the preceding token (\+) zero or one time (i.e., optional) | 1234567890 |
[0-9] |
Character class: matches any single digit (0-9) | 1 |
{10,15} |
Quantifier: matches the preceding token ([0-9]) between 10 and 15 times (inclusive) | 1234567890 |
$ |
Anchor: asserts position at end of the string | +123456789012345 |
Matches
123456789012345678901+1234567890+123456789012345
Does not match
+1234567891234567890123456abc12345678901234567890+123-456-7890
Common pitfalls
- Forgetting to escape the '+' character, which in regex has a special meaning (one or more of the preceding token).
- Assuming the pattern matches international phone numbers with country codes (e.g., '+1-555-123-4567').
- Not accounting for strings with leading or trailing whitespace (anchors ^ and $ do not ignore whitespace).
Performance notes
The pattern is efficient with no catastrophic backtracking risk. Anchors (^ and $) ensure the entire string is evaluated, which is optimal for this use case.