Regex library
Regular expression for US ZIP codes
Match US ZIP and ZIP+4 codes. UK postcodes such as SW1A 1AA are rejected, which is useful on mixed forms.
^\d{5}(?:-\d{4})?$
Runs 100% in your browser. Nothing you paste is uploaded, stored, or sent to a server.
Highlighted matches
How to use
- Scan the pre-filled pattern and the sample. Hits are highlighted; misses stay plain.
- Toggle flags if you need global or case-insensitive matching. The pattern is JavaScript / ECMAScript.
- Paste your own corpus. Nothing is sent to a server.
- Read the notes below before you drop the pattern into production — several of these are intentionally incomplete.
A US ZIP code is five digits, optionally followed by a hyphen and four more. That is the entire public shape. This pattern does not check whether the code is assigned by USPS.
What this pattern will not do
Spaces instead of a hyphen in ZIP+4 will fail; normalize first.
Leading zeros (02115 for Boston) are preserved because the value is digits, not a number.
FAQ
Can I require ZIP+4?
Use ^\d{5}-\d{4}$ without the optional group.
What about Canadian postal codes?
They are letter-digit-letter-space-digit-letter-digit. Different pattern, different page someday.
Is the sample text uploaded?
No. The tester compiles the pattern with the browser’s RegExp engine and stays on the device.