Advertisement

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.

The pattern
^[0-9]{5}(-[0-9]{4})?$
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
12345Match5-digit ZIPTry →
12345-6789MatchZIP+4Try →
1234No matchonly 4 digitsTry →
123456No match6 digitsTry →
zip 12345No matchanchors force full matchTry →

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.
Open the regex tester →

FAQ

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.