Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^(?:\d{3}-?\d{2}-?\d{4})$

Matches a string representing a social security number (SSN) in the format 123-45-6789 or 123456789, with optional hyphens between digit groups.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the position at the start of the string Start of '123-45-6789'
(?: Non-capturing group start Starts grouping for the SSN pattern
\d Digit character class (matches any digit 0-9) Digit '1' in '123'
{3} Quantifier: exactly 3 occurrences of the preceding token Matches '123' in '123-45-6789'
-? Optional hyphen (matches 0 or 1 hyphen) Hyphen '-' in '123-45-6789'
\d Digit character class (matches any digit 0-9) Digit '4' in '45'
{2} Quantifier: exactly 2 occurrences of the preceding token Matches '45' in '123-45-6789'
-? Optional hyphen (matches 0 or 1 hyphen) Hyphen '-' in '45-6789'
\d Digit character class (matches any digit 0-9) Digit '6' in '6789'
{4} Quantifier: exactly 4 occurrences of the preceding token Matches '6789' in '123-45-6789'
) Non-capturing group end Ends grouping for the SSN pattern
$ Anchor: asserts the position at the end of the string End of '123-45-6789'

Matches

  • 123456789
  • 123-45-6789
  • 000123456
  • 999-88-7777

Does not match

  • 12-345-6789
  • 1234-56-789
  • 12345678
  • 123-45-678
  • 1234567890
  • abc-def-ghij
  • 123--45-6789
  • 123-45--6789

Common pitfalls

  • Assuming the pattern validates SSN validity (e.g., does not check for invalid SSN ranges like 000, 666, or 900-999 in the first group).
  • Not accounting for leading/trailing whitespace (use trim() in JavaScript before matching).
  • Misunderstanding that the hyphens are optional but must not appear in invalid positions (e.g., '123--45-6789' fails).
  • Confusing this pattern with stricter SSN validation rules (e.g., some systems require hyphens in specific positions).

Performance notes

The pattern is efficient with no catastrophic backtracking risk due to fixed quantifiers and no nested quantifiers. Anchors (^ and $) ensure 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 (3 digits, optional hyphen, 2 digits, optional hyphen, 4 digits). It does not validate against real SSN rules (e.g., first group cannot be 000, 666, or 900-999).

Why use a non-capturing group (?:...) instead of a capturing group?

Non-capturing groups are more efficient as they do not store the matched substring in memory for backreferences or extraction. This pattern does not need to capture the SSN parts.

How would I modify this to require hyphens?

Replace the optional hyphens (-?) with literal hyphens (-): ^(?:\d{3}-\d{2}-\d{4})$