DD
DevDash

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

InputResult
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"
end

php

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

Want API access + no ads? Pro coming soon.