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
9410310001-123402134
Doesn't match
941094103394103-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 falsePython
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