validation

US ZIP Code Regex Pattern

Validates US ZIP codes — either 5-digit basic (12345) or 9-digit ZIP+4 extended format (12345-6789). Does NOT validate that the code corresponds to a real postal area.

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

What each part matches

^\d{5} — five required digits
(-\d{4})? — optional group: dash + 4 more digits (ZIP+4)

✓ These match

  • 90210
  • 10001-2345
  • 00501

✗ These don't

  • 9021
  • 90210-
  • 90210-234
  • ABCDE

Use in your code

JavaScript

const re = /^\d{5}(-\d{4})?$/;
re.test(input); // → true or false

Python

import re
re.fullmatch(r"^\d{5}(-\d{4})?$", input)

PHP (PCRE)

preg_match('/^\d{5}(-\d{4})?$/', $input);

Go

re := regexp.MustCompile(`^\d{5}(-\d{4})?$`)
re.MatchString(input)

FAQ

Does this us zip code regex work in JavaScript?
Yes. Every pattern in the Utilko regex library is tested to work in JavaScript RegExp, PCRE (PHP, Nginx), and Python `re`. Where flavor matters (lookbehind, named groups), the pattern page flags it.
How do I use this pattern?
Copy the pattern from the code block above. In JavaScript: new RegExp('^\d{5}(-\d{4})?$') or literal /^\d{5}(-\d{4})?$/. Or click "Try in regex tester" to open it pre-loaded in Utilko's browser-based regex tester.
Should I use this for security-critical validation?
Client-side regex is fine for UX (immediate feedback on a form). For anything security-critical — payments, auth, data integrity — always re-validate server-side using the same pattern PLUS domain-specific checks (Luhn checksum for cards, actual email delivery test, DNS resolution for domains).

Related regex patterns

← All regex patterns · Regex tester · Regex cheat sheet