Regex to Match US Zip Codes

Validate US ZIP codes with this regex pattern. Matches both 5-digit and ZIP+4 extended formats like 90210 or 10001-1234. Free online regex tester.

Regular Expression

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

Token Breakdown

TokenDescription
^Anchors at the start of the string (or line in multiline mode)
\dMatches any digit (0-9)
{5}Matches exactly 5 times
(?:Start of non-capturing group
-Matches the literal character '-'
\dMatches any digit (0-9)
{4}Matches exactly 4 times
)End of group
?Makes the preceding element optional (zero or one times)
$Anchors at the end of the string (or line in multiline mode)

Detailed Explanation

This regex validates US ZIP codes in both the standard 5-digit format and the extended ZIP+4 format. Here is the token-by-token breakdown:

^ — Anchors the match at the start of the string.

\d{5} — Matches exactly five digits for the basic ZIP code. The \d shorthand matches any digit 0-9, and {5} requires exactly five occurrences. US ZIP codes range from 00501 (Holtsville, NY) to 99950 (Ketchikan, AK).

(?:-\d{4})? — An optional non-capturing group for the ZIP+4 extension. When present, it matches a literal hyphen followed by exactly four digits. The ZIP+4 code provides more specific location information, narrowing down to a specific delivery route or building. The ? makes the entire group optional.

$ — Anchors the match at the end of the string.

This pattern matches ZIP codes like 90210, 10001, 00501, 10001-1234, and 99950-0001. It validates the format (correct number of digits) but does not check whether the ZIP code actually exists or corresponds to a real location.

The pattern is intentionally strict about the format: exactly 5 digits, optionally followed by a hyphen and exactly 4 more digits. It does not accept spaces, other separators, or partial codes. This is appropriate for form validation where you want users to enter properly formatted ZIP codes. For international postal codes, different patterns are needed as formats vary widely by country.

Example Test Strings

InputExpected
90210Match
10001-1234Match
1234No Match
123456No Match
00501Match

Try It — Interactive Tester

//
gimsuy
No matches found.
Pattern: 18 charsFlags: noneMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →