Pattern library / Numbers & Codes
Regex to Match US ZIP Codes (5-Digit & ZIP+4)
\d{5} matches any five digits anywhere. This is the anchored version that accepts 12345 and 12345-6789 but rejects partials and garbage.
How the pattern works
^[0-9]{5}— exactly five digits at the start.(-[0-9]{4})?— optional hyphen + 4 digits for ZIP+4.- For non-US postal codes, swap the digit groups to match the country's format.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
12345 | Match | 5-digit ZIP | Try → |
12345-6789 | Match | ZIP+4 | Try → |
1234 | No match | only 4 digits | Try → |
123456 | No match | 6 digits | Try → |
zip 12345 | No match | anchors force full match | Try → |
Where to use it
JavaScript
const re = /^[0-9]{5}(-[0-9]{4})?$/;
re.test('12345-6789'); // true
Python
import re
re.match(r'^[0-9]{5}(-[0-9]{4})?$', '12345')
Common mistakes
Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
FAQ
Related patterns
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.