ZIP code regex

Regex for US ZIP and ZIP+4 codes, plus UK postcodes and Canadian postal codes. Tested examples.

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

Matches

  • 94103
  • 10001-1234
  • 02134

Doesn't match

  • 9410
  • 941033
  • 94103-12

Alternative: Canadian postal code

^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z] ?\d[ABCEGHJ-NPRSTV-Z]\d$

Notes

  • Store ZIP codes as strings — leading zeros (New England) are significant.
  • UK postcodes are irregular; a common pragmatic pattern is ^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$ with the i flag.

Code

JavaScript

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

Python

import re

bool(re.fullmatch(r'\d{5}(?:-\d{4})?', value))

Try it on your own input

The live tester adds the g and m flags so every line is checked separately.

Questions

Does a matching ZIP exist?

Not necessarily. The regex checks format; USPS data is needed to check a ZIP is assigned.

More patterns

Email regexPhone number regexURL regexIP address regexDate regexTime regex (24-hour and 12-hour)UUID regexHex color regex