Regex for US ZIP Code
Regex Pattern
^\d{5}(?:-\d{4})?$5-digit ZIP or ZIP+4 format
Quick Answer
The regex pattern for us zip code is `^\d{5}(?:-\d{4})?$`. 5-digit ZIP or ZIP+4 format. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| 12345 | ✓ Matches |
| 90210 | ✓ Matches |
| 12345-6789 | ✓ Matches |
| 1234 | ✗ No match |
| 123456 | ✗ No match |
| ABCDE | ✗ No match |
Code Examples
javascript
const regex = /^\d{5}(?:-\d{4})?$/;
const isValid = regex.test(value);python
import re
pattern = r'^\d{5}(?:-\d{4})?$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^\d{5}(?:-\d{4})?$/
if value =~ pattern
puts "valid"
endphp
if (preg_match('/^\d{5}(?:-\d{4})?$/', $value)) {
echo "valid";
}java
String pattern = "^\\d{5}(?:-\\d{4})?$";
boolean isValid = value.matches(pattern);Frequently Asked Questions
Related Regex Patterns
UK Postcode
UK postcode format
Indian PIN Code
6-digit Indian PIN code
Canadian Postal Code
Canadian postal code (letter-digit-letter digit-letter-digit)
Email Address
Validates a standard email address format
Email (RFC 5322 Compliant)
RFC 5322 compliant email validation with label length limits
URL (HTTP/HTTPS)
Validates HTTP and HTTPS URLs