Match and validate US ZIP codes in 5-digit and ZIP+4 formats using regex. Ready-to-use patterns for address validation.
/\b\d{5}(?:-\d{4})?\b/g| Token | Meaning |
|---|---|
\b | Word boundary to prevent partial matches |
\d{5} | Exactly 5 digits (standard ZIP code) |
(?:-\d{4})? | Optional ZIP+4 extension: dash followed by 4 digits |
\b | Trailing word boundary |
90210Match10001Match90210-1234Match00501Match1234No match123456No matchABCDENo matchconst zipRegex = /\b\d{5}(?:-\d{4})?\b/g;
const text = "Ship to 90210 or 10001-5432 for delivery.";
const matches = text.match(zipRegex);
console.log(matches); // ["90210", "10001-5432"]import re
zip_regex = r'\b\d{5}(?:-\d{4})?\b'
text = "Ship to 90210 or 10001-5432 for delivery."
matches = re.findall(zip_regex, text)
print(matches) # ['90210', '10001-5432']Want to test this pattern with your own data?
Try this pattern in Regex Tester →