T
ToolPrime

Regex for US ZIP Code

Validates US ZIP codes in 5-digit or ZIP+4 format.

Pattern

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

Live Tester

Enter a string to test

Examples

10001
90210
12345-6789
1234
123456
ABCDE

Pattern Breakdown

^\d{5} — five digits

(-\d{4})?$ — optional dash + four more digits

Code Snippets

Javascript

const regex = /^\d{5}(-\d{4})?$/;
regex.test("90210"); // true

Python

import re
pattern = r"^\d{5}(-\d{4})?$"
bool(re.match(pattern, "90210"))  # True

Php

$pattern = '/^\d{5}(-\d{4})?$/';
preg_match($pattern, "90210"); // 1

Related Patterns

Frequently Asked Questions

What is the regex for US ZIP Code?
The regex pattern for US ZIP Code is /^\d{5}(-\d{4})?$/. Validates US ZIP codes in 5-digit or ZIP+4 format.
How do I use the US ZIP Code regex in JavaScript?
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the US ZIP Code pattern match?
It matches strings like 10001; it rejects strings like 1234. See the Examples section above for the full list of matching and non-matching cases.

Related Tools