Download Cheat sheet PDF 12 pages · syntax, editors, patterns, Unicode, performance, debugging

Regex for us zip code

Match a US ZIP code in 5-digit or ZIP+4 (5-4) format.

The us zip code regex is ^\d{5}(-\d{4})?$ — copy it below, or open it in the explainer for a token-by-token breakdown.

The pattern

^\d{5}(-\d{4})?$

Try it in the explainer → Download cheat sheet ↓

What it matches

  • 12345
  • 90210
  • 12345-6789

What it doesn't match

  • 1234
  • 123456
  • ABCDE
  • 12345-678

Token-by-token breakdown

Every part of the pattern, left to right:

TokenMeaning
^start of string (or line in multiline mode)
\d{5}exactly 5 times: digit (0–9)
(start of a capturing group
-literal text “-”
\d{4}exactly 4 times: digit (0–9)
)?end of group, optional (zero or one)
$end of string (or line in multiline mode)

Edge cases & gotchas

  • Doesn't verify the ZIP code corresponds to a real US location
  • Some ZIP codes start with 0 (e.g. 02134 in Massachusetts) — pattern handles this correctly
  • Doesn't accept ZIP codes with spaces instead of hyphens (12345 6789)
  • For Canadian postal codes (A1A 1A1) or UK postcodes, use entirely different patterns

Per-flavor variants

The pattern is identical across JavaScript, Python, and PCRE for this case. If you need to convert a flavor-specific pattern, use the flavor converter.

JavaScript^\d{5}(-\d{4})?$
Python^\d{5}(-\d{4})?$
PCRE^\d{5}(-\d{4})?$

When NOT to use this regex

For address validation or normalization, use a postal-service API (USPS API, SmartyStreets, Lob). They verify the ZIP exists, attach a city/state, and confirm a deliverable address — which regex cannot do.

Standards & sources

This pattern is based on the following authoritative specification(s) and issuing authorities. Formats can change — always confirm against the primary source.

Related patterns

More patterns in the United States 🇺🇸 category:

Related