US ZIP Code
Match US ZIP codes in 5-digit (12345) and ZIP+4 (12345-6789) formats.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("\\b\\d{5}(?:[\\-\\s]\\d{4})?\\b", "g");
const input = "90210";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"\b\d{5}(?:[\-\s]\d{4})?\b")
input_text = "90210"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\b\d{5}(?:[\-\s]\d{4})?\b`)
input := `90210`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
\b\d{5}(?:[\-\s]\d{4})?\b (flags: g)Raw source: \b\d{5}(?:[\-\s]\d{4})?\b
How it works
Examples
Input
90210Matches
90210
Input
10001-1234Matches
10001-1234
Input
1234No match
—Common use cases
- •Address form validation
- •Shipping/logistics data processing
- •Geographic data extraction
- •E-commerce checkout validation
Related patterns
Canadian Postal Code
ValidationMatch Canadian postal codes in the A1A 1A1 or A1A1A1 format with valid first-letter prefixes.
International Phone Number (Loose)
ValidationMatch international phone numbers in a variety of loose formats including country codes, area codes, and separators.
ISO 3166-1 alpha-2 Country Code
ValidationValidate 2-letter ISO 3166-1 alpha-2 country codes (US, GB, FR, JP, etc.) — structural check only.
ISO 4217 Currency Code
ValidationValidate 3-letter ISO 4217 currency codes (USD, EUR, GBP, JPY, etc.) — structural check only.
Related concepts
Word Boundaries: \b and \B
Concept\b matches the position between a word character and a non-word character. It keeps your regex from matching 'cat' inside 'concatenate.'
How to Match Digits in Regex
How-toUse \d for any digit, [0-9] for ASCII digits only, or {n} to match a specific count of digits. Combine with anchors for whole-string validation.
How to Match an Optional Group
How-toPut a ? after a character, group, or character class to make it optional. Groups with ? match zero or one occurrence of the whole group.
How to Match a Specific Number of Characters
How-toUse {n} for exactly n, {n,} for n or more, {n,m} for between n and m. Apply to any single token — character, class, or group.