Validation
Match a US ZIP code
Match 5-digit US ZIP codes with optional +4 extension.
Pattern
Regular expression
\b[0-9]{5}(?:-[0-9]{4})?\bHow it works
Matches exactly 5 digits, optionally followed by `-` and 4 more digits. Word boundaries prevent partial matches inside longer numbers.
Matches
- ▸94103
- ▸10001-1234
- ▸20500
Non-matches
- ▸1234 (too short)
- ▸123456 (too long)
- ▸94103-1
Language snippets
JavaScript
const re = /\b[0-9]{5}(?:-[0-9]{4})?\b/g;Python
re.findall(r"\b[0-9]{5}(?:-[0-9]{4})?\b", text)Go
re := regexp.MustCompile(`\b[0-9]{5}(?:-[0-9]{4})?\b`)Java
Pattern.compile("\\b[0-9]{5}(?:-[0-9]{4})?\\b")Related patterns
Match an email address
[\w.+-]+@[\w-]+\.[\w.-]+
Match a URL
https?:\/\/[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=]*
Match an IPv4 address
(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)
Match an IPv6 address
(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|::1|::
Match a UUID (v4)
[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}
Match a US phone number
(?:\+?1[-. ]?)?\(?[2-9][0-9]{2}\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}