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
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| \d | Matches any digit (0-9) |
| {5} | Matches exactly 5 times |
| (?: | Start of non-capturing group |
| - | Matches the literal character '-' |
| \d | Matches 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
| Input | Expected |
|---|---|
| 90210 | Match |
| 10001-1234 | Match |
| 1234 | No Match |
| 123456 | No Match |
| 00501 | Match |
Try It — Interactive Tester
18 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match US Phone Numbers
/^(?:\+1\s?)?(?:\(?\d{3}\)?[\s.-]?)?\d{3}[\s.-]?\d{4}$/
Regex to Match Social Security Numbers
/^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/
Regex to Match Currency Amounts
/^\$\d{1,3}(?:,\d{3})*(?:\.\d{2})?$/
Regex to Match IPv4 Addresses
/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/g