Regex for us zip code
Match a US ZIP code in 5-digit or ZIP+4 (5-4) format.
The pattern
^\d{5}(-\d{4})?$
What it matches
123459021012345-6789
What it doesn't match
1234123456ABCDE12345-678
How it works
Token-by-token breakdown:
^- Anchor to the start.
\d{5}- Exactly 5 digits — the base ZIP code.
(-\d{4})?- Optional: a hyphen followed by exactly 4 more digits (ZIP+4 extension).
$- Anchor to the end.
Edge cases & gotchas
- Doesn't verify the ZIP code corresponds to a real US location
- Some ZIP codes start with 0 (e.g. 02134 in Massachusetts) — pattern handles this correctly
- Doesn't accept ZIP codes with spaces instead of hyphens (
12345 6789) - For Canadian postal codes (A1A 1A1) or UK postcodes, use entirely different patterns
Per-flavor variants
The pattern is identical across JavaScript, Python, and PCRE for this case. If you need to convert a flavor-specific pattern, use the flavor converter.
| JavaScript | ^\d{5}(-\d{4})?$ |
| Python | ^\d{5}(-\d{4})?$ |
| PCRE | ^\d{5}(-\d{4})?$ |
When NOT to use this regex
For address validation or normalization, use a postal-service API (USPS API, SmartyStreets, Lob). They verify the ZIP exists, attach a city/state, and confirm a deliverable address — which regex cannot do.