Regex for date (YYYY-MM-DD)

/\b\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])\b/g

Options
Matches

Examples

Note

Checks month and day ranges but not the calendar: 2024-02-31 still matches. Parse the match with a date library to be sure.

Matches in the example

  • "2024-01-15"
  • "2024-12-31"

Token by token

  1. \b a word boundary
  2. \d{4} a digit (0–9), exactly 4 times
  3. - the character “-”
  4. (?: a group, not capturing
  5. 0 the character “0”
  6. [1-9] one of: “1” to “9”
  7. | or
  8. 1 the character “1”
  9. [0-2] one of: “0” to “2”
  10. ) end of group
  11. - the character “-”
  12. (?: a group, not capturing
  13. 0 the character “0”
  14. [1-9] one of: “1” to “9”
  15. | or
  16. [12] one of: “1” or “2”
  17. \d a digit (0–9)
  18. | or
  19. 3 the character “3”
  20. [01] one of: “0” or “1”
  21. ) end of group
  22. \b a word boundary

Other patterns