Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^\d{2}/\d{2}/\d{4}$

Matches a date string in the format DD/MM/YYYY where each component is exactly two or four digits. The pattern enforces strict formatting with slashes as separators.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the position at the start of the string '' (before '12/31/2023')
\d Digit character class: matches any single digit (0-9) '1' in '12/31/2023'
{2} Quantifier: exactly 2 occurrences of the preceding token (\d) '12' in '12/31/2023'
/ Literal forward slash character: matches exactly '/' '/' in '12/31/2023'
\d Digit character class: matches any single digit (0-9) '3' in '12/31/2023'
{2} Quantifier: exactly 2 occurrences of the preceding token (\d) '31' in '12/31/2023'
/ Literal forward slash character: matches exactly '/' '/' in '12/31/2023'
\d Digit character class: matches any single digit (0-9) '2' in '12/31/2023'
{4} Quantifier: exactly 4 occurrences of the preceding token (\d) '2023' in '12/31/2023'
$ Anchor: asserts the position at the end of the string '' (after '12/31/2023')

Matches

  • 01/01/2000
  • 31/12/1999
  • 09/09/2023

Does not match

  • 1/1/2000
  • 01-01-2000
  • 01/01/200
  • 01/01/20000
  • 01/01/20a0
  • 01/01/2000
  • 01/01/2000

Common pitfalls

  • Forgets to escape the forward slashes, causing syntax errors
  • Assumes the pattern validates actual date values (e.g., '31/02/2023' is syntactically valid but not a real date)
  • Ignores that leading zeros are required (e.g., '1/1/2000' fails)
  • Overlooks that the pattern does not account for whitespace around the date

Performance notes

The pattern is linear and has no quantifiers with variable lengths, so it poses no risk of catastrophic backtracking. Anchors ensure the entire string is matched, preventing partial matches.

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

Does this pattern validate actual calendar dates?

No, it only validates the format DD/MM/YYYY with digits. It does not check if the date is valid (e.g., '31/02/2023' would match syntactically but is not a real date).

Why are the slashes not escaped in the pattern?

In JavaScript regex, forward slashes do not need to be escaped when they are not used as regex delimiters. However, if the regex is written as a string (e.g., in code), the slashes must be escaped to avoid syntax errors.

Can this pattern match dates with single-digit days or months?

No, the pattern requires exactly two digits for the day and month (e.g., '01/01/2000' matches, but '1/1/2000' does not).