Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02])\1|(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

Matches valid dates in DD/MM/YYYY, DD-MM-YYYY, or DD.MM.YYYY formats, including leap year handling for February 29th.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: start of string Start of input
(?: Non-capturing group start Grouping for alternation
(?: Non-capturing group start Grouping for 31-day months
31 Literal '31' Day 31
(\/|-|\.) Capture group 1: separator (/, -, or .) Separator character
(?:0?[13578]|1[02]) Non-capturing group: months with 31 days (1,3,5,7,8,10,12) Months like 1, 01, 3, 12
\1 Backreference to capture group 1 (separator) Same separator as first
| Alternation (OR) Either 31-day or 29/30-day month
(?:29|30) Non-capturing group: days 29 or 30 Day 29 or 30
(\/|-|\.) Capture group 2: separator (/, -, or .) Separator character
(?:0?[1,3-9]|1[0-2]) Non-capturing group: months with 29/30 days (1-9,10,11,12) Months like 2, 02, 4, 11
\2 Backreference to capture group 2 (separator) Same separator as second
)) Non-capturing group end (twice) End of 31/29/30-day month group
(?:(?:1[6-9]|[2-9]\d)?\d{2}) Non-capturing group: year (1600-9999) Year like 1999, 2024
$ Anchor: end of string End of input
| Alternation (OR) Either first pattern or leap year February
^ Anchor: start of string Start of input
(?:29(\/|-|\.)0?2\3 Non-capturing group: Feb 29 with separator Feb 29 with separator
(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))) Non-capturing group: leap year validation (divisible by 4, not by 100 unless by 400) Years like 2000, 2024, 2028
$ Anchor: end of string End of input
| Alternation (OR) Either previous patterns or standard date
^ Anchor: start of string Start of input
(?:0?[1-9]|1\d|2[0-8]) Non-capturing group: days 1-28 Day 1-28
(\/|-|\.) Capture group 4: separator (/, -, or .) Separator character
(?:(?:0?[1-9])|(?:1[0-2])) Non-capturing group: months 1-12 Months like 1, 01, 12
\4 Backreference to capture group 4 (separator) Same separator as fourth
(?:(?:1[6-9]|[2-9]\d)?\d{2}) Non-capturing group: year (1600-9999) Year like 1999, 2024
$ Anchor: end of string End of input

Matches

  • 31/01/2024
  • 30-04-2023
  • 15.05.1999
  • 29/02/2020
  • 29-02-2000
  • 01/01/1600
  • 28.12.9999

Does not match

  • 31/04/2024
  • 30-02-2023
  • 29/02/2023
  • 32/01/2024
  • 00/01/2024
  • 15/13/2024
  • 29/02/1900
  • 29.02.2100

Common pitfalls

  • Fails to validate years below 1600 (e.g., 1599 is accepted but invalid in this pattern)
  • Does not handle years with leading zeros beyond single-digit (e.g., 01600 is invalid)
  • Separator must be consistent throughout the date (e.g., 31/01-2024 fails)
  • Does not account for dates with extra whitespace or formatting
  • Leap year validation may not cover all edge cases (e.g., 2100 is not a leap year but may be accepted if not properly validated)

Performance notes

High risk of catastrophic backtracking due to nested quantifiers and alternations. Avoid using this pattern on untrusted input without strict validation. Consider simplifying or breaking into smaller patterns.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Why does the pattern not validate years below 1600?

The year part uses (1[6-9]|[2-9]\d)?\d{2} which starts from 1600. Years below 1600 would require a different pattern (e.g., [0-9]{4}).

Can this pattern match dates with different separators in the same string?

No. The pattern enforces consistent separators via backreferences (\1, \2, \3, \4).

Does this handle all leap years correctly?

The leap year logic covers most cases but may not handle all historical calendar rules (e.g., Julian vs. Gregorian calendar transitions).

Why are there three separate alternatives in the pattern?

The three alternatives handle: (1) 31-day months, (2) February 29th (leap years), and (3) all other valid dates (1-28 days).

Is this pattern safe for user input validation?

No. Due to its complexity and backtracking risk, it is not recommended for untrusted input. Use a dedicated date library instead.