Regex Explainer

Regex for a YYYY-MM-DD date

Named capture groups for the parts, and why this validates shape but not calendar validity.

/^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$/

Match a whole string consisting of (exactly 4 of a digit) captured as "year", then "-", then (exactly 2 of a digit) captured as "month", then "-", then (exactly 2 of a digit) captured as "day".

What to check

  • Note

    Anchors match string ends, not line ends

    Without the m flag, ^ and $ match only the very start and end of the input — not the start and end of each line. On multi-line input this is often not what was intended.

    Fix: Add the m flag if the pattern should apply per line.

Step by step

  1. ^

    Anchor to the start of the input.

  2. (?<year>\d{4})

    Match (exactly 4 of a digit) captured as "year".

  3. -

    Match "-".

  4. (?<month>\d{2})

    Match (exactly 2 of a digit) captured as "month".

  5. -

    Match "-".

  6. (?<day>\d{2})

    Match (exactly 2 of a digit) captured as "day".

  7. $

    Anchor to the end of the input.

Details

Capture groups
3 (named: year, month, day)
Flags
None set

Check your own pattern

Paste one on the home page, or call the API or MCP server. This page is also available as markdown.

Related examples