Regex to Match a US ZIP Code
Copy the pattern, paste your text below, and see every US ZIP it finds. Below the tester: how it accepts both 5-digit and ZIP+4 forms.
Regex pattern
/\b\d{5}(?:-\d{4})?\b/g
This matches a US ZIP code in 5-digit (12345) or ZIP+4 (12345-6789) form. It checks the number format only, not whether the ZIP actually exists.
What this pattern matches
This matches a US ZIP code: five digits, optionally followed by a hyphen and the four-digit ZIP+4 extension.
How it works, part by part
\b\d{5} — exactly five digits on a word boundary.
(?:-\d{4})? — an optional hyphen and four more digits for ZIP+4.
\b — a trailing boundary so a six-digit number is not accepted.
Shape only
This validates the format, not whether the ZIP actually exists. To confirm a real, deliverable ZIP you need a postal database lookup.
Use it in your code
import re
text = "Ship to 90210 or the full ZIP+4 90210-1234. Not a ZIP: 1234 (too short), 902101 (six digits)."
pattern = r'''\b\d{5}(?:-\d{4})?\b'''
for m in re.finditer(pattern, text):
print(m.group())
const text = "Ship to 90210 or the full ZIP+4 90210-1234. Not a ZIP: 1234 (too short), 902101 (six digits).";
const re = new RegExp(String.raw`\b\d{5}(?:-\d{4})?\b`, "g");
console.log(text.match(re));
FAQ
Does it match ZIP+4?
Yes. 90210-1234 matches via the optional -#### group.
Will 902101 match?
No. The word boundary rejects a six-digit run that is not a valid 5 or 5+4 shape.
Does it check the ZIP is real?
No. It only checks the format; use a postal lookup to confirm the ZIP exists.