★ Identity & contact · regex
Regex for US ZIP Code
Matches 5-digit US ZIP codes and the ZIP+4 format (12345-6789). Leading zeros are kept, as in New England ZIPs.
- Runs in your browser
- No signup
Try the us zip code pattern
Read it in plain English (best-effort)
- ^start of a line
- [0-9]one character from: 0–9
- {5}exactly 5 of the previous
- (?:start a group (not captured)
- -the character '-'
- [0-9]one character from: 0–9
- {4}exactly 4 of the previous
- )end of group
- ?the previous is optional
- $end of a line
One sample per line works well with the m flag on anchored patterns.
The pattern, piece by piece
/^[0-9]{5}(?:-[0-9]{4})?$/| Part | What it does |
|---|---|
^[0-9]{5} | exactly five digits |
(?:-[0-9]{4})? | optional hyphen and four more digits (ZIP+4) |
$ | end of the string |
What it accepts and rejects
Every sample below is checked automatically in JavaScript and Python. The tester above is preloaded with all of them, one per line.
| Should match | |
|---|---|
94105 | ✓ |
10001 | ✓ |
02134 | ✓ |
94105-1234 | ✓ |
00501 | ✓ |
99950 | ✓ |
| Should not match | |
|---|---|
9410 | ✗ |
941051 | ✗ |
94105-123 | ✗ |
94105 1234 | ✗ |
ABCDE | ✗ |
94105- | ✗ |
Copy it into your code
RE2-safeNo lookarounds or backreferences, so the same pattern works in Go's RE2 engine and in Rust's regex crate.
const usZipRe = /^[0-9]{5}(?:-[0-9]{4})?$/;
export function isValidUsZip(value) {
return usZipRe.test(value.trim());
}When not to use regex for this
Store ZIPs as strings, never numbers, or 02134 becomes 2134. To check a ZIP is real or matches a city, use a postal data API.
Questions engineers actually ask
Why store ZIP codes as text?
Does it accept spaces in ZIP+4?
What is catastrophic backtracking?
More identity & contact patterns
- US Phone NumberNANP numbers in common formats, capturing area code, exchange and line.^(?:\+?1[ .-]?)?\(?([2-9][0-9]{2})\)?[ .-]?([2-9][0-9]{2})[ .-]?([0-9]{4})$
- E.164 Phone NumberInternational numbers in E.164: +, country code, up to 15 digits.^\+[1-9][0-9]{6,14}$
- Username3–16 characters, starts with a letter, letters, digits and underscores only.^[a-zA-Z][a-zA-Z0-9_]{2,15}$
- Strong Password12+ characters with lowercase, uppercase, a digit and a symbol.^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{12,}$
- Credit Card Number Format16-digit cards in groups of four, and 15-digit Amex in 4-6-5. Format only.^(?:(?:\d{4}[ -]?){3}\d{4}|3[47]\d{2}[ -]?\d{6}[ -]?\d{5})$
Or browse the full regex library.
More from the workbench
Fine print
Tools: on the house.
Engineer: by the hour.
The regex was free. Code that doesn't need one is the upgrade: typed schemas, validated once at the edge, trusted everywhere after.
I'm Himanshu, an AI and full-stack engineer. I build MVPs, AI agents and the growth plumbing around them for early-stage startups.
