Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^\d{3} \d{3} \d{4}$

Matches a string that starts with three digits, followed by a space, then three more digits, another space, and ends with four digits. Typically used for phone number formats like US area codes.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string '' (position before first character)
\d Digit character class (0-9) 0, 1, 2, ..., 9
{3} Quantifier: exactly 3 occurrences of the preceding token 123
Literal space character
\d Digit character class (0-9) 0, 1, 2, ..., 9
{3} Quantifier: exactly 3 occurrences of the preceding token 456
Literal space character
\d Digit character class (0-9) 0, 1, 2, ..., 9
{4} Quantifier: exactly 4 occurrences of the preceding token 7890
$ Anchor: asserts position at end of the string '' (position after last character)

Matches

  • 123 456 7890
  • 000 000 0000
  • 987 654 3210

Does not match

  • 12 345 6789
  • 1234 567 890
  • 123-456-7890
  • 123 4567 890
  • 123 456 789
  • 123 456 78901
  • abc 456 7890
  • 123 456 789a

Common pitfalls

  • Forgetting to escape the space character (should be ' ', not '\s') if literal space is intended.
  • Using this pattern for international phone numbers without country codes.
  • Assuming this matches all phone number formats (e.g., (123) 456-7890 would not match).
  • Not anchoring the pattern (^ and $) may lead to partial matches in longer strings.

Performance notes

This pattern is efficient with no risk of catastrophic backtracking. Anchors (^ and $) ensure the entire string is checked, preventing partial matches. No nested quantifiers or complex groups are present.

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

No. This pattern is designed for a specific format (three digits, space, three digits, space, four digits) and does not account for country codes or different separators.

Can I use this pattern to validate US phone numbers?

Partially. This pattern matches the common US format (e.g., 123 456 7890), but US phone numbers often include parentheses, dashes, or country codes (e.g., +1 (123) 456-7890). A more comprehensive pattern would be needed.

Why use literal spaces instead of \s?

The pattern uses literal spaces (' ') to match only space characters. Using \s would also match tabs or other whitespace characters, which may not be intended for this specific format.

How can I make this pattern case-insensitive?

This pattern does not contain letters, so case-insensitivity is irrelevant. If extended to include letters (e.g., for alphanumeric formats), the 'i' flag could be used in JavaScript.