Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^\d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{2}:\d{2}(?:\.\d{1,6})?(?:Z|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9])?$

Matches ISO 8601 datetime strings with optional fractional seconds and timezone offset, from start to end of string.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of '2023-01-01T12:00:00Z'
\d{4} Exactly 4 digits (year) 2023
- Literal hyphen character -
\d{1,2} 1 or 2 digits (month) 01
- Literal hyphen character -
\d{1,2} 1 or 2 digits (day) 01
T Literal 'T' character separating date and time T
\d{1,2} 1 or 2 digits (hour in 24-hour format) 12
: Literal colon character :
\d{2} Exactly 2 digits (minutes) 00
: Literal colon character :
\d{2} Exactly 2 digits (seconds) 00
(?:\.\d{1,6})? Non-capturing group: optional fractional seconds (1 to 6 digits) preceded by a dot .123456
(?:Z|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9])? Non-capturing group: optional timezone, either 'Z' or ±HH:MM where HH is 00-23 and MM is 00-59 Z or +05:30
$ Anchor: asserts position at end of the string End of '2023-01-01T12:00:00Z'

Matches

  • 2023-01-01T12:00:00Z
  • 2023-12-31T23:59:59.999999+05:30
  • 1999-01-01T00:00:00-12:00
  • 2000-02-29T00:00:00.1Z

Does not match

  • 2023-1-1T12:00:00Z
  • 2023-01-01T12:00:00
  • 2023-01-01T12:00:00.1234567Z
  • 2023-01-01T24:00:00Z
  • 2023-01-01T12:00:00+24:00
  • 2023-01-01T12:00:00.123456Zextra

Common pitfalls

  • Forgetting the ^ and $ anchors can lead to partial matches within longer strings
  • The month/day/hour/minute/second components must be zero-padded to 2 digits where applicable (e.g., '01' not '1')
  • The timezone offset must be in the format ±HH:MM with valid hour (00-23) and minute (00-59)
  • Fractional seconds must not exceed 6 digits

Performance notes

The pattern is linear and does not contain nested quantifiers that could cause catastrophic backtracking. The use of non-capturing groups (?:...) improves performance by avoiding unnecessary capture overhead.

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 (e.g., February 30)?

No, this pattern only validates the format and ranges of individual components (e.g., month 01-12, day 01-31, hour 00-23). It does not validate whether the date is logically valid (e.g., February 30).

Can this pattern match timestamps without a timezone?

No, the pattern requires either 'Z' or a timezone offset (±HH:MM) at the end. If optional timezone is desired, the last non-capturing group should be made optional with a ? quantifier.

Is the fractional seconds part mandatory?

No, the fractional seconds part is optional due to the ? quantifier following the non-capturing group.