Regex for US ZIP Codes
US ZIP codes are five digits, optionally followed by a hyphen and the four-digit ZIP+4 add-on. The group makes the extension all-or-nothing, so a dangling hyphen or a short suffix fails.
/^\d{5}(?:-\d{4})?$/How it works, token by token
| Token | Meaning |
|---|---|
| ^ | start of string |
| \d{5} | exactly five digits |
| (?:-\d{4})? | optionally, a hyphen and four more digits (ZIP+4) |
| $ | end of string |
What it matches
✓
90210✓
10001-1234✓
02134✗
9021✗
90210-✗
90210-12Try it live
One candidate per line — the m flag makes the ^ and $ anchors apply to each line. Edit anything; it runs in your browser.
/
Variations
^\d{5}(?:[-\s]?\d{4})?$also accept a space, or no separator, before the +4^\d{5}$five-digit ZIP codes onlyLanguage notes
- In Python 3 and .NET, \d also matches digits from other scripts by default. Use [0-9], or Python's re.ASCII flag, for strict ASCII.
Frequently asked questions
Why keep ZIP codes as strings?
Codes in New England start with 0 — 02134 is in Boston. Store them as strings, never numbers, or the leading zero disappears.
Does a match mean the ZIP code exists?
No — 00000 matches. To confirm a real ZIP code, check it against USPS data or an address-validation service.
Related patterns
Building something custom? The regex tester gives you live match highlighting for any pattern.