Use ^\d{5}$ for 5-digit ZIP codes, ^\d{5}-\d{4}$ for ZIP+4, and ^\d{5}(?:-\d{4})?$ for either. Pick by what your form accepts, then check the pattern on your own codes in the Regex Generator. It highlights matches and counts them live in your browser.
Which ZIP code regex do you need: 5-digit, ZIP+4 or both?
The table below is regex logic, not tool output. The tool's own literal may add lookarounds, as in the ZIP+4 literal shown in the test results.
| Variant | Pattern | Accepts | Rejects | Fits |
|---|---|---|---|---|
| 5-digit | ^\d{5}$ |
02134 | 1234, 123456 | Checkout, signup |
| ZIP+4 | ^\d{5}-\d{4}$ |
12345-6789 | 12345 | Lookups that need the full 9 digits |
| Both | ^\d{5}(?:-\d{4})?$ |
12345 and 12345-6789 | 12345- | Free-form address fields |
How the ZIP code regex works, piece by piece
\d{5} means exactly five digits. That alone is the 5-digit pattern. For ZIP+4 you add a literal hyphen and \d{4}: four more digits.
The combined pattern wraps the hyphen and the last four digits in an optional group, (?:-\d{4})?. The ? makes the whole group optional, so 12345 and 12345-6789 pass. A lone 12345- fails, because the group is all or nothing.
Anchors versus lookarounds
Use ^ and $ to validate a whole field; they require the entire value to be a ZIP code.
To search text, anchors do not work. In Ship to 02134 today, the ZIP code sits in the middle, so ^…$ finds nothing. Use a lookbehind (?<!\d) and a lookahead (?!\d) instead. They mean "no digit directly before" and "no digit directly after" without consuming characters, which keeps \d{5} from matching the first five digits of a longer number.
The ZIP+4 literal the tool produced in our test uses this approach: /(?<!\d)\d{5}-\d{4}(?!\d)/g.
Lookbehind is not available in every JavaScript engine. If you cannot rely on it, capture the code with /(?:^|\D)(\d{5}(?:-\d{4})?)(?!\d)/g and read group 1 of each match.
How to generate a ZIP code regex with Toolper
- Open the Regex Generator.
- Pick the US ZIP template, then choose the variant that matches your form.
- Set the anchors and flags. Turn on
^…$to validate a whole field, or leave it off to search text. The anchors and the g, i and m flags switch on and off independently. - Paste your test codes into the text box. Include a code with a leading zero, a value with a missing digit, and a code with a trailing hyphen.

- Read the "N matches" counter and the highlighted matches. If a code you expected is not highlighted, change the variant or the anchors.
- Press Copy to put the
/pattern/flagsliteral on your clipboard. The tool also shows the bare pattern string for use innew RegExp(...).

To send the pattern to a colleague, use the share link. The state lives in the address hash, so it opens without a server. Your template, options and text are also saved in this browser.
Validate a ZIP code in JavaScript
Form validation with RegExp.test. Anchor the pattern and leave off the g flag:
const zip = /^\d{5}(?:-\d{4})?$/;
zip.test("02134"); // true
zip.test("12345-6789"); // true
zip.test("12345-"); // false
One value with String.match. Use it when you want the matched text, not just yes or no:
"Ship to 12345-6789".match(/(?<!\d)\d{5}(?:-\d{4})?(?!\d)/);
Every ZIP in a block of text with matchAll. This requires the g flag:
const re = /(?<!\d)\d{5}(?:-\d{4})?(?!\d)/g;
const zips = [...text.matchAll(re)].map(m => m[0]);
Results of our test
| Template | Test text | Regex literal | Expected matches | Found matches | Highlighted | Copy exact |
|---|---|---|---|---|---|---|
| Email address | Write to a@b.com or x.y@mail.org, not foo@ | /[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:.[A-Za-z0-9-]+)*.[A-Za-z]{2,}/g | 2 | 2 | 2 | yes |
| US phone number | Call (555) 123-4567 or (212) 555-0100 today | /(?<!\d)(?:+1[\s.-]?)?(?:(\d{3}) | \d{3})[\s.-]?\d{3}[\s.-]?\d{4}(?!\d)/g | 2 | 2 | 2 |
| IPv4 address | 10.0.0.1 and 999.1.1.1 | /(?<!\d)(?<!\d.)(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}(?!\d)(?!.\d)/g | 1 | 1 | 1 | yes |
| Hex color | Brand #ABCDEF, accent #1a2 and bad #12 | /#(?:[0-9A-Fa-f]{6}|[0-9A-Fa-f]{3})(?![0-9A-Fa-f])/gi | 2 | 2 | 2 | yes |
| US ZIP code (5+4 digits) | 12345 and 12345-6789 | /(?<!\d)\d{5}-\d{4}(?!\d)/g | 1 | 1 | 1 | yes |
We ran each template in Chromium with Playwright: we picked the template, set the flags, pasted the test text, read the match counter and the highlighted matches, then pressed Copy and read the clipboard.
The counter matched the expected count in all five runs, the highlight count matched the counter every time, and Copy put exactly the displayed literal on the clipboard. For the ZIP+4 variant, 12345 and 12345-6789 gave 1 match: the full 12345-6789, with the bare 12345 skipped. Everything ran in the browser, and our test text was never sent to a server.
We ran only the ZIP+4 variant. The 5-digit and combined patterns above are regex logic, not tool output, so check them on your own codes in the tool. The tool also caps the length of the text and the number of matches it will process, so it is not meant for scanning very large logs in one pass.
Common ZIP validation mistakes
- No boundaries. An unanchored
\d{5}matches the first five digits of1234567, andabc12345xyzpasses as valid on a form field. Anchors fix this for fields; lookarounds fix it for text. - A careless optional group. With the hyphen outside the group, as in
\d{5}-?(\d{4})?,12345-slips through. Keep the hyphen inside:(?:-\d{4})?. - Lost leading zeros. Stored as a number,
02134becomes2134, which no five-digit pattern accepts. Keep ZIPs as strings from the form field to the database. For the same idea in spreadsheets, see how to keep leading zeros by storing values as text. - Stray whitespace. Pasted values often carry a leading or trailing space. Trim before you test, or the anchored pattern rejects a valid code.
- Using the g flag with
test(). A global regex remembers its position between calls, so the same input can return true, then false. Drop g for validation and keep it formatchAll. - Expecting a regex to prove the ZIP exists. A pattern checks the shape only.
00000matches but may not be a real code. For that you need a separate address check. - Using a ZIP+4-only pattern on a general field. Valid 5-digit input gets rejected.
FAQ
What is the regex for a US zip code?
^\d{5}$ for the standard 5-digit code. It accepts 02134 and rejects 1234 and 123456. To also accept ZIP+4, use ^\d{5}(?:-\d{4})?$.
How do you validate a ZIP+4 code with regex?
Use ^\d{5}-\d{4}$: five digits, a hyphen, four digits, anchored at both ends. In our test, the ZIP+4 variant on 12345 and 12345-6789 found 1 match, the full 9-digit value.
What regex matches both 5-digit and 9-digit ZIP codes?
^\d{5}(?:-\d{4})?$. It accepts 12345 and 12345-6789 and rejects 12345-. In the Regex Generator, choose the "any" ZIP variant.
How do you find ZIP codes inside a longer text with regex?
Replace ^ and $ with (?<!\d) and (?!\d), add the g flag, and call text.matchAll(re).
Should ZIP codes be stored as numbers or strings?
Strings. A number drops the leading zero, so 02134 becomes 2134.
Generate your ZIP code pattern: paste your own codes, watch the match counter, and copy the exact /pattern/flags literal.
Updated November 27, 2026
