本页面有中文版。
Cette page est disponible en français.
Esta página está disponible en español.

Real-world validators

Match the formats you meet every day — integers, decimals, hex colours, times, dates, emails, slugs, IPs, ZIPs and more.

Read the rules: Common patterns in the Guide →Drill it: Format validators →

Builds on Lookarounds

What you'll learn

Numbers Hex colours Times & dates Emails Slugs & IPs

Steps · 14

  1. 01Validating the real worldBriefing
  2. 02Whole numbersHunt
  3. 03Decimal numbersHunt
  4. 04Hex coloursHunt
  5. 0524-hour timeHunt
  6. 06ISO datesHunt
  7. 07Simple emailsHunt
  8. 08URL slugsHunt
  9. 09IPv4 addressesHunt
  10. 10US ZIP codesHunt
  11. 11HashtagsHunt
  12. 12UsernamesHunt
  13. 13Golf: short hexHunt
  14. 14Validators completeBriefing

Regex validation patterns

A validation regex answers one question: does this text have the right shape? Sign-up forms, spreadsheets and config files are full of values that should look a certain way, such as a time, a date, a colour code or an email address. Wrap the pattern in ^ and $ and it must match the whole value, so stray spaces or extra characters fail the check.

Each format is built from small parts. \d is a digit, {4} asks for exactly four, ? makes a part optional, and a group such as (am|pm) lists the allowed choices. Number ranges need the most care: (100|[1-9]?\d) covers 0 to 100, while \d+ would also let 250 through. In each hunt on this track you write one validator that catches the good values and spares the near-misses.

Be honest about what a regex can know. It checks format, not meaning. A date pattern happily accepts 31/02/2026 unless you write extra branches for each month, and an email pattern can't tell whether the inbox exists. Use the regex as a fast first filter, then let real code check the calendar, send a confirmation email or look up the record.

Examples

  1. /^(0?[1-9]|1[0-2]):[0-5]\d ?(am|pm)$/i

    Catches

    • 9:30 am
    • 12:05PM
    • 07:45 pm

    Spares

    • 13:00 pm
    • 9:60 am
    • 9:30

    A 12-hour time: hours 1 to 12 with an optional leading zero, and the i flag accepts both pm and PM.

  2. /^(0[1-9]|[12]\d|3[01])/(0[1-9]|1[0-2])/\d{4}$/

    Catches

    • 05/11/2026
    • 31/12/1999
    • 31/02/2026

    Spares

    • 5/11/2026
    • 12/13/2026
    • 32/01/2026

    Day and month are range-checked, yet 31/02/2026 still passes, because the pattern knows shapes, not calendars.

  3. /^[A-Z]{3}-\d{4}$/

    Catches

    • ABC-1234
    • XYZ-0042

    Spares

    • AB-1234
    • abc-1234
    • ABC-12345
    • ABC1234

    A product code: exactly three capital letters, a dash and four digits, with the anchors refusing anything longer.

  4. /^(100|[1-9]?\d)%$/

    Catches

    • 0%
    • 42%
    • 100%

    Spares

    • 101%
    • 07%
    • 42

    [1-9]?\d covers 0 to 99 without a leading zero, and 100 gets a branch of its own.

Common mistakes

  • Forgetting the anchors. Without ^ and $, a pattern for four digits also passes 12345 and abc2026, because it finds a match somewhere inside.
  • Leaving the dot bare. In a decimal or a domain, . matches any character, so 3x14 slips through a number check; write \. when you mean a real dot.
  • Chasing every edge case in one pattern. A fully standard email regex runs to hundreds of characters; a short pattern plus a confirmation email is easier to read and catches more real mistakes.

Read the rules: Common patterns in the Guide →Drill it: Format validators →

Read more: Email regex: copy-ready patterns that actually work →

Read more: Regex for IPv4 addresses: matching 0 to 255 properly →