Regex for US ZIP code

/\b\d{5}(?:-\d{4})?\b/g

Options
Matches

Examples

Note

Five digits, optionally with a ZIP+4 extension. It cannot tell a ZIP from any other five-digit number.

Matches in the example

  • "94103"
  • "10001-1234"

Token by token

  1. \b a word boundary
  2. \d{5} a digit (0–9), exactly 5 times
  3. (?: a group, not capturing
  4. - the character “-”
  5. \d{4} a digit (0–9), exactly 4 times
  6. )? end of group, whole group optionally
  7. \b a word boundary

Other patterns