Token-by-token breakdown
| Token | Meaning | Example |
|---|
^ |
Anchor: asserts position at start of the string |
+1234567890 |
\+? |
Optional literal '+' character (0 or 1 occurrence) |
+1234567890 |
[0-9\s\-] |
Character class matching any digit (0-9), whitespace, or hyphen |
1 2-3 4567890 |
{10,15} |
Quantifier: matches between 10 and 15 of the preceding token (character class) |
1234567890 (10 chars) |
$ |
Anchor: asserts position at end of the string |
1234567890 |
Matches
+1234567890123 456 7890123-456-7890123456789012345+123 456-78901234567890123456789012
Does not match
+1234567891234567890123456abc12345678901234567890 1234567890+123456789011234567890-
Common pitfalls
- Forgetting to escape the '+' in JavaScript (must be '\+').
- Assuming the pattern allows more than 15 characters due to quantifier misinterpretation.
- Overlooking that the pattern does not enforce a specific format (e.g., phone number structure).
- Ignoring that the pattern allows leading/trailing spaces if not anchored properly (though '$' prevents this).
Performance notes
No catastrophic backtracking risk. The pattern is linear in complexity (O(n) where n is string length). Anchors (^ and $) ensure efficient matching by preventing partial matches.
About regular expressions
Regular expressions are tiny programs that match patterns in text. They're powerful and
notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq
Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one
does, and runs through both matching and non-matching examples so you can verify the logic.
Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in
syntax and supported features. Pick your flavor in the input — the explainer accounts for it.
Frequently asked
Does this pattern validate phone numbers?
No, it only checks for a string of 10-15 characters containing digits, spaces, or hyphens, optionally starting with '+'. It does not enforce phone number formatting rules.
Why is the '+' escaped as '\+' in JavaScript?
In JavaScript regex, '+' is a metacharacter (quantifier). To match a literal '+', it must be escaped with a backslash.
Can this pattern match strings with leading or trailing spaces?
No, because the anchors (^ and $) ensure the entire string matches the pattern, and the pattern does not include leading/trailing spaces.
What happens if the string is shorter than 10 characters?
The pattern will not match because the quantifier {10,15} requires at least 10 characters.