Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches strings representing dates in YYYY-MM-DD format, ensuring exactly four digits, a hyphen, two digits, another hyphen, and two more digits, from start to end of the string.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the position at the start of the string Start of '2023-12-31'
\d Character class: matches any digit (0-9) Digit '2' in '2023-12-31'
{4} Quantifier: specifies exactly 4 occurrences of the preceding token Four digits '2023' in '2023-12-31'
- Literal hyphen character Hyphen '-' in '2023-12-31'
\d Character class: matches any digit (0-9) Digit '1' in '12'
{2} Quantifier: specifies exactly 2 occurrences of the preceding token Two digits '12' in '2023-12-31'
- Literal hyphen character Hyphen '-' in '2023-12-31'
\d Character class: matches any digit (0-9) Digit '3' in '31'
{2} Quantifier: specifies exactly 2 occurrences of the preceding token Two digits '31' in '2023-12-31'
$ Anchor: asserts the position at the end of the string End of '2023-12-31'

Matches

  • 2023-12-31
  • 0000-00-00
  • 9999-99-99
  • 1234-56-78

Does not match

  • 2023/12/31
  • 2023-12-3
  • 2023-12-311
  • 2023-13-01
  • 2023-12-3a
  • 2023-12-31
  • 2023-12-31
  • 2023-12-31-extra

Common pitfalls

  • Does not validate date logic (e.g., '2023-02-30' is matched but invalid)
  • Does not account for leading/trailing whitespace (use \s* or trim input first)
  • Does not handle alternative date separators (e.g., '/')
  • Does not enforce month/day ranges (e.g., month 13 or day 32)
  • Fails on strings with extra characters before/after the date

Performance notes

No catastrophic backtracking risk due to fixed quantifiers and simple structure. Anchors (^ and $) ensure the regex matches the entire string, improving efficiency. For large inputs, consider anchoring to avoid 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 regex validate actual dates (e.g., February 30th)?

No, it only checks the format (4 digits, hyphen, 2 digits, hyphen, 2 digits). Additional validation is required for logical dates (e.g., using a library or custom logic).

How do I allow optional whitespace around the date?

Modify the pattern to ^\s*\d{4}-\d{2}-\d{2}\s*$ to trim whitespace before/after the date.

Can this regex match dates with slashes instead of hyphens?

No, but you could use an alternation like ^\d{4}[-/]\d{2}[-/]\d{2}$ to allow either hyphens or slashes.

Why does '2023-13-01' match this regex?

Because the regex only checks the format (digits and hyphens), not the logical validity of the month (13) or day.