Regex for a US ZIP Code

Matches standard 5-digit ZIP codes and the extended ZIP+4 format.

Published October 11, 2026

This regex matches US ZIP codes in the 5-digit form and the ZIP+4 form.

How the pattern works

Breaking the pattern down: \d{5} = five digits; (-\d{4})? = optional plus-four.

Test your regex against both matching and non-matching inputs; use the Regex tool on this site to experiment live.

Pattern tests

/^\d{5}(-\d{4})?$/
✓ '90210'
✓ '90210-1234'
✗ '9021'
✗ '90210-12'

Practical examples

The pattern

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

Test it

const re = /^\d{5}(-\d{4})?$/
re.test('90210')   // true
re.test('90210-1234')   // true
re.test('9021')   // false
re.test('90210-12')   // false

In Python

import re
re.search(r'^\d{5}(-\d{4})?$', text)

Common mistakes

  • Storing ZIP as a number (loses leading zeros).
  • Rejecting ZIP+4.
  • Assuming digits mean a real ZIP.

Key takeaways

  • Anchors (^ and $) make the whole string match instead of a part of it.
  • Regex checks format only; verify data separately when it matters.
  • Test with valid and invalid samples.

FAQ

Why keep as text?

ZIPs like 02134 start with zero.

Where can I test this pattern?

Use the Regex Tester on this site, or regex101.com.

More General articles