Regexdrill

Patterns for common data Updated November 2026

Regex for date YYYY-MM-DD

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ matches dates like 2024-03-15: four digits, a month 01 to 12 and a day 01 to 31. It checks the shape and digit ranges, not the calendar (2024-02-31 passes), so parse the string with Date afterwards when validity matters.

Syntax

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

Examples

["2024-03-15", "2024-13-01", "2024-3-5"].map(s => /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/.test(s))

Result: [true, false, false]

Month 13 fails the range, 2024-3-5 fails because months and days need two digits.

"Due 2024-03-15".match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/).groups

Result: { year: "2024", month: "03", day: "15" }

"03/15/2024".replace(/^(\d{2})\/(\d{2})\/(\d{4})$/, "$3-$1-$2")

Result: "2024-03-15"

Reorder captured groups to convert MM/DD/YYYY to ISO.

"from 2024-01-01 to 2024-12-31".match(/\d{4}-\d{2}-\d{2}/g)

Result: ["2024-01-01", "2024-12-31"]

Common mistakes

  • \d{4}-\d{2}-\d{2} accepts 2024-99-99; add the month and day alternations or validate with a date parser.
  • Unescaped slashes in a JavaScript literal like /\d{2}/\d{2}/ end the regex early; write \/ or use new RegExp with a string.
  • Without anchors, 12024-03-156 matches in the middle; add ^ and $ when validating a field.
  • Expecting the regex to reject February 30 or non-leap-year February 29; it cannot, so check with new Date() or a date library.

Practice Regex for date YYYY-MM-DD

These drills check your answer on live data, free, no account needed.

  • Dates YYYY-MM-DDThe whole line must match. Match a date written YYYY-MM-DD with a month from 01 to 12 and a day from 01 to 31. Do not worry about month lengths.
  • Named groups: year, month, dayFind the first date written YYYY-MM-DD and capture its parts in named groups called year, month and day.
  • Reformat MM/DD/YYYY as YYYY-MM-DDRewrite every date written MM/DD/YYYY as YYYY-MM-DD. Leave everything else unchanged.

Questions

How do I validate a date with regex?

Match the shape with ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$, then build a Date from the parts and confirm the month and day did not roll over. A regex alone cannot know how many days each month has.

What is the regex for DD/MM/YYYY?

^(0[1-9]|[12]\d|3[01])\/(0[1-9]|1[0-2])\/\d{4}$. Swap the first two groups for MM/DD/YYYY. Escape the slashes in a JavaScript literal.

Does the regex check leap years?

No. Patterns that try to encode leap years exist but are hundreds of characters long and still wrong for edge cases. Use the regex for format, then a date parser for validity.

Related

  • Regex for time HH:MMA time regex for 24-hour HH:MM from 00:00 to 23:59, a 12-hour version with AM or PM, how to add optional seconds, and how to pull times out of log lines.
  • Named groups (?<name>...)How to capture by name, read match.groups.name, use $<name> in replacements and \k<name> as a backreference, and the (?P<name>) syntax Python needs.
  • Ranges in character classesHow character ranges work by code point, how to combine them, the invalid [a-Z] and surprising [A-z], and how to express numeric ranges with alternation.
  • QuantifiersWhat * + ? {n} {n,} and {n,m} mean, how greedy matching works, how to repeat a group instead of a single character, and the silent {2, 3} mistake.

Previous: Regex for phone numberNext: Regex for URL