Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:,\d{3})?$

Matches a date-time string in the format YYYY-MM-DD HH:MM:SS with optional milliseconds (comma followed by 3 digits). The entire string must match from start to end.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string before '2023' in '2023-10-05 14:30:00'
\d Digit character class (0-9) Any digit like '2' in '2023'
{4} Quantifier: exactly 4 occurrences of the preceding token Four digits '2023'
- Literal hyphen character Hyphen between '2023' and '10'
\d Digit character class (0-9) Any digit like '1' in '10'
{2} Quantifier: exactly 2 occurrences of the preceding token Two digits '10'
- Literal hyphen character Hyphen between '10' and '05'
\d Digit character class (0-9) Any digit like '0' in '05'
{2} Quantifier: exactly 2 occurrences of the preceding token Two digits '05'
Literal space character Space between date and time
\d Digit character class (0-9) Any digit like '1' in '14'
{2} Quantifier: exactly 2 occurrences of the preceding token Two digits '14'
: Literal colon character Colon between '14' and '30'
\d Digit character class (0-9) Any digit like '3' in '30'
{2} Quantifier: exactly 2 occurrences of the preceding token Two digits '30'
: Literal colon character Colon between '30' and '00'
\d Digit character class (0-9) Any digit like '0' in '00'
{2} Quantifier: exactly 2 occurrences of the preceding token Two digits '00'
(?: Non-capturing group start (does not capture matched text) Start of optional milliseconds group
, Literal comma character Comma before milliseconds
\d Digit character class (0-9) Any digit like '1' in '123'
{3} Quantifier: exactly 3 occurrences of the preceding token Three digits '123'
) Non-capturing group end End of optional milliseconds group
? Quantifier: 0 or 1 occurrence of the preceding non-capturing group Optional presence of ',123'
$ Anchor: asserts position at end of the string End of string after '00' or ',123'

Matches

  • 2023-10-05 14:30:00
  • 2023-12-31 23:59:59
  • 2000-01-01 00:00:00,123
  • 1999-09-09 09:09:09,999

Does not match

  • 2023-10-5 14:30:00
  • 2023-10-05 14:30:0
  • 2023-10-05 14:30:00,12
  • 2023-10-05 14:30:00,1234
  • 2023/10/05 14:30:00
  • 2023-10-05T14:30:00
  • 2023-10-05 14:30:00

Common pitfalls

  • Forgetting to escape the hyphens and colons in some regex flavors (not needed in JavaScript for these literals)
  • Assuming the milliseconds part is mandatory (it is optional due to the ? quantifier)
  • Not anchoring the pattern with ^ and $, which could lead to partial matches in longer strings
  • Confusing \d with \D (non-digit) or misusing character classes

Performance notes

The pattern is linear in complexity with no nested quantifiers or alternations, so catastrophic backtracking is not a concern. Anchoring with ^ and $ ensures the entire string is validated, improving efficiency. The non-capturing group (?:...) is used for grouping without capturing, which is slightly more efficient than a capturing group.

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 use a non-capturing group (?:...) instead of a capturing group (...)?

The non-capturing group is used here because the milliseconds part is optional and does not need to be captured for backreferences or extraction. It avoids unnecessary memory usage and processing overhead.

Can this pattern match dates outside the valid range (e.g., month 13)?

No, this pattern does not validate the logical correctness of the date (e.g., month 13 or day 32). It only checks the format. Additional validation would be required for logical correctness.

How would I modify this pattern to require the milliseconds part?

Remove the ? quantifier after the non-capturing group: ^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:,\d{3})$