Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^\+?[0-9\s\-\(\)]{10,15}$

Matches strings of 10 to 15 characters that may start with an optional '+', followed by digits, spaces, hyphens, or parentheses.

Token-by-token breakdown

TokenMeaningExample
^ 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

  • +1234567890
  • 123 456 7890
  • (123)456-7890
  • 123-456-7890
  • 1234567890
  • ++123456789012345
  • +123 456 7890

Does not match

  • +123456789
  • 12345678901
  • +1234567890123456
  • abc1234567890
  • 123-456-789
  • 12345678901234567

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.

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 international phone numbers?

No, it only checks for a string of 10-15 characters that may include digits, spaces, hyphens, and parentheses, optionally starting with '+'. It does not enforce any specific phone number format.

Can this pattern match a string with more than 15 characters?

No, the {10,15} quantifier strictly limits the match to 10-15 characters.

Why does the pattern allow parentheses and hyphens?

The character class [0-9\s\-\(\)] explicitly includes these characters, allowing for common phone number formatting characters.