Regex for US ZIP code (5-digit and ZIP+4)

US ZIP codes are five digits, optionally followed by a dash and four more digits (ZIP+4). This pattern accepts both.

The pattern

/\b\d{5}(?:-\d{4})?\b/g

The tester opens with this pattern and the examples below already loaded. Nothing you type is uploaded.

How it works

\d{5}
The five-digit ZIP code.
(?:-\d{4})?
Optional dash and four-digit extension.
\b
Word boundaries so six-digit numbers do not match.

Examples

Matches

  • 94103
  • 10001-1234
  • 60614

Does not match

  • 1234
  • 123456
  • 1234-5678

Use it in JavaScript

const re = /\b\d{5}(?:-\d{4})?\b/g;

// Find every match in a block of text
const found = text.match(re);

// Validate that a whole value matches (no stray characters around it)
const exact = new RegExp('^(?:' + re.source + ')$', re.flags.replace('g', ''));
const isValid = exact.test(value);

Limits and tips

  • It only checks the shape. Some five-digit strings are not assigned to any area.
  • For the Indian equivalent, see the PIN code pattern. It has six digits and cannot start with 0.

Frequently asked questions

What is ZIP+4?

An extended ZIP code with a dash and four more digits that narrows delivery to a block or building.

Does it match Canadian postal codes?

No. Those alternate letters and digits, for example K1A 0B1, and need a different pattern.