How the pattern works
| Part | Meaning |
|---|---|
^\d{5} | Five digits |
(?:-\d{4})? | Optional ZIP+4 extension: a dash and four digits |
$ | End of the string |
Code examples
JavaScript
const re = /^\d{5}(?:-\d{4})?$/;
re.test(value); // true or falsePython
import re
bool(re.fullmatch(r'\d{5}(?:-\d{4})?', value))PHP
preg_match('/^\d{5}(?:-\d{4})?$/', $value) === 1;
Notes and limitations
Keep ZIP codes as strings, not numbers — codes in New England start with 0 and would lose it.
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 ZIP code?
/^\d{5}(?:-\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: 90210, 10001-1234, 02134. 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.