Contact
US ZIP Code Regex
US ZIP codes have five digits, and the extended ZIP+4 form adds a dash and four more digits for more precise delivery. This pattern accepts both and nothing else, which makes it a reliable check for shipping addresses, tax calculators and store locators. Leading zeros are allowed because ZIP codes in New England begin with 0, so always store them as strings rather than numbers. The honest limit is that it accepts any five digits, including ranges the postal service has never assigned, so use an address service when you need deliverability.
Pattern (JavaScript)
/^[0-9]{5}(?:-[0-9]{4})?$/Starts at the start of the line/string, then a digit, exactly 5 times, then (the character "-", then a digit, exactly 4 times), optionally (zero or one time), then the end of the line/string.
Limit: Checks the format only; it cannot tell whether the ZIP code is assigned.
Examples
Matches
- ✓ 90210
- ✓ 10001-1234
- ✓ 02134
Does not match
- ✗ 9021
- ✗ 90210-12
- ✗ ABCDE
Try it
0 matches (JavaScript engine)
90210
10001-1234
02134
9021
90210-12
ABCDECode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[0-9]{5}(?:-[0-9]{4})?$/g;
const text = "90210";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[0-9]{5}(?:-[0-9]{4})?$')
text = '90210'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[0-9]{5}(?:-[0-9]{4})?$/';
$text = '90210';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[0-9]{5}(?:-[0-9]{4})?$");
String text = "90210";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[0-9]{5}(?:-[0-9]{4})?$`)
text := "90210"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[0-9]{5}(?:-[0-9]{4})?$");
var text = "90210";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[0-9]{5}(?:-[0-9]{4})?$").unwrap();
let text = "90210";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[0-9]{5}(?:-[0-9]{4})?$/
text = '90210'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why should ZIP codes be stored as text?
- Codes such as 02134 start with zero, and converting them to numbers silently drops that digit. Keep them as strings in forms, databases and spreadsheets.
- Can I accept ZIP+4 without the dash?
- Change -[0-9]{4} to -?[0-9]{4} to accept nine digits in a row. Be aware this also accepts any nine-digit number, so the dashed form is clearer.
- Does this check that the ZIP code matches the state?
- No. Matching a ZIP code to a state or city requires a lookup table or an address verification service. The regex only checks the format.