Regex to Match Social Security Numbers

Validate US Social Security Numbers (SSN) format with this regex pattern. Checks the XXX-XX-XXXX format with invalid range exclusions. Free tool.

Regular Expression

/^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/

Token Breakdown

TokenDescription
^Anchors at the start of the string (or line in multiline mode)
(?!Start of negative lookahead assertion
0Matches the literal character '0'
0Matches the literal character '0'
0Matches the literal character '0'
|Alternation — matches the expression before OR after the pipe
6Matches the literal character '6'
6Matches the literal character '6'
6Matches the literal character '6'
|Alternation — matches the expression before OR after the pipe
9Matches the literal character '9'
\dMatches any digit (0-9)
{2}Matches exactly 2 times
)End of group
\dMatches any digit (0-9)
{3}Matches exactly 3 times
-Matches the literal character '-'
(?!Start of negative lookahead assertion
0Matches the literal character '0'
0Matches the literal character '0'
)End of group
\dMatches any digit (0-9)
{2}Matches exactly 2 times
-Matches the literal character '-'
(?!Start of negative lookahead assertion
0Matches the literal character '0'
0Matches the literal character '0'
0Matches the literal character '0'
0Matches the literal character '0'
)End of group
\dMatches any digit (0-9)
{4}Matches exactly 4 times
$Anchors at the end of the string (or line in multiline mode)

Detailed Explanation

This regex validates US Social Security Numbers (SSN) in the standard XXX-XX-XXXX format with exclusions for known invalid ranges. Here is the token-by-token breakdown:

^ — Anchors the match at the start of the string.

(?!000|666|9\d{2}) — A negative lookahead that rejects invalid area numbers. The SSA has never issued SSNs starting with 000 or 666, and numbers starting with 9XX were reserved for the Individual Taxpayer Identification Number (ITIN) program. This lookahead checks ahead without consuming characters.

\d{3} — Matches exactly three digits for the area number (first three digits).

  • — Matches a literal hyphen separator.

(?!00) — A negative lookahead rejecting group numbers of 00, which are not valid in real SSNs.

\d{2} — Matches exactly two digits for the group number (middle two digits).

  • — Matches a literal hyphen separator.

(?!0000) — A negative lookahead rejecting serial numbers of 0000, which are not valid.

\d{4} — Matches exactly four digits for the serial number (last four digits).

$ — Anchors the match at the end of the string.

This pattern enforces the correct format and excludes ranges that the Social Security Administration has declared invalid. Note that this is a format validation only; it does not verify that the SSN has actually been issued. Handle SSN data with appropriate security measures, as it is sensitive personally identifiable information.

Example Test Strings

InputExpected
123-45-6789Match
000-12-3456No Match
666-12-3456No Match
900-12-3456No Match
555-55-5555Match

Try It — Interactive Tester

//
gimsuy
No matches found.
Pattern: 51 charsFlags: noneMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →