How the pattern works
| Part | Meaning |
|---|---|
(?!000|666|9\d\d)\d{3} | Area number: three digits, but never 000, 666 or 900–999 |
(?!00)\d{2} | Group number: never 00 |
(?!0000)\d{4} | Serial number: never 0000 |
- | Dashes between the parts |
Code examples
JavaScript
const re = /^(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/;
re.test(value); // true or falsePython
import re
bool(re.fullmatch(r'(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}', value))PHP
preg_match('/^(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/', $value) === 1;
Notes and limitations
Negative lookaheads rule out the number ranges the Social Security Administration never issues. Treat SSNs as sensitive data: mask them in logs and interfaces.
Regex TesterType a pattern and watch matches highlight live in your test text.
Open the tool Frequently asked questions
What is the regex for US Social Security number?
/^(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/ — it matches the whole string. See the table above for what each part does.
Which examples match?
Of the examples tested, these match: 123-45-6789, 219-09-9999. The others don’t.
How do I test this regex?
Click “Open in Regex Tester” — the pattern, flags and sample text load automatically, and matches highlight as you edit.