Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches a string representing a US Social Security Number (SSN) in the format XXX-XX-XXXX, where X is a digit. The pattern enforces the exact structure with hyphens separating three groups of digits.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the position at the start of the string Start of '123-45-6789'
\d Character class: matches any digit (0-9) Digit '3' in '123-45-6789'
{3} Quantifier: specifies exactly 3 occurrences of the preceding token Three digits '123' in '123-45-6789'
- Literal hyphen character: matches a single hyphen Hyphen '-' in '123-45-6789'
\d Character class: matches any digit (0-9) Digit '4' in '123-45-6789'
{2} Quantifier: specifies exactly 2 occurrences of the preceding token Two digits '45' in '123-45-6789'
- Literal hyphen character: matches a single hyphen Hyphen '-' in '123-45-6789'
\d Character class: matches any digit (0-9) Digit '6' in '123-45-6789'
{4} Quantifier: specifies exactly 4 occurrences of the preceding token Four digits '6789' in '123-45-6789'
$ Anchor: asserts the position at the end of the string End of '123-45-6789'

Matches

  • 123-45-6789
  • 000-00-0000
  • 999-99-9999

Does not match

  • 123456789
  • 12-345-6789
  • 123-456-789
  • 123-45-678
  • 123-45-67890
  • abc-def-ghij
  • 123 45 6789
  • 123-45-678A

Common pitfalls

  • Assuming the pattern validates real SSNs (it only checks format, not validity).
  • Forgetting to escape the hyphens (they are literal characters).
  • Not anchoring the pattern (^ and $) may lead to partial matches in longer strings.
  • Confusing \d with [0-9] (both work in JavaScript, but \d is more readable).

Performance notes

The pattern is linear in complexity (O(n)) and has no risk of catastrophic backtracking due to fixed quantifiers and no alternations or nested quantifiers. Anchoring ensures the entire string is matched, 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 real SSNs?

No, it only checks the format (XXX-XX-XXXX). It does not validate against a list of real SSNs or check for invalid ranges (e.g., 666 in the first group).

Can I use this pattern to extract SSNs from a larger string?

No, because the pattern is anchored with ^ and $. To extract SSNs from a larger string, remove the anchors and use a global flag (e.g., /\d{3}-\d{2}-\d{4}/g).

Why use \d instead of [0-9]?

\d is more concise and readable, though both work in JavaScript. \d is also more portable across regex flavors.

Does this pattern match SSNs with leading zeros?

Yes, the pattern allows leading zeros (e.g., '000-00-0000' is a valid match).