Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

This regex matches ISO 8601 formatted date-time strings with optional fractional seconds and timezone offset, ensuring valid date and time components.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string
\d{4} Exactly 4 digits (0-9) 2023
- Literal hyphen character -
(?:0[1-9]|1[0-2]) Non-capturing group for month: 01-09 or 10-12 05 or 12
- Literal hyphen character -
(?:0[1-9]|[12][0-9]|3[01]) Non-capturing group for day: 01-09, 10-29, or 30-31 07 or 25 or 31
T Literal 'T' character separating date and time T
(?:[01][0-9]|2[0-3]) Non-capturing group for hour: 00-19 or 20-23 08 or 23
: Literal colon character :
[0-5][0-9] Exactly 2 digits for minutes: 00-59 30
: Literal colon character :
[0-5][0-9] Exactly 2 digits for seconds: 00-59 45
(?:\.\d{1,6})? Optional non-capturing group for fractional seconds: dot followed by 1 to 6 digits .123456 or .1
(?:Z|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9])? Optional non-capturing group for timezone: either 'Z' or ±HH:MM Z or +05:30 or -08:00
$ Anchor: asserts position at end of the string End of string

Matches

  • 2023-05-07T14:30:45Z
  • 2023-12-31T23:59:59.999999+05:30
  • 1999-01-01T00:00:00-08:00
  • 2000-02-29T12:00:00.123456Z

Does not match

  • 2023-13-01T12:00:00Z
  • 2023-02-30T12:00:00Z
  • 2023-05-07T24:00:00Z
  • 2023-05-07T14:60:00Z
  • 2023-05-07T14:30:60Z
  • 2023-05-07T14:30:45.1234567Z
  • 2023-05-07T14:30:45+24:00
  • 2023-05-07T14:30:45+05:60

Common pitfalls

  • Forgetting to escape the hyphen in character classes if not at start/end
  • Assuming the regex validates real calendar dates (e.g., 2023-02-29 is invalid but passes)
  • Overlooking that the timezone offset must be ±HH:MM with valid hour/minute ranges
  • Not accounting for the optional fractional seconds or timezone parts
  • Assuming the regex matches all valid ISO 8601 formats (e.g., it does not support '2023-05-07T14:30:45+0530' without colon)

Performance notes

The regex is linear in complexity due to non-capturing groups and alternations with fixed-length patterns. No catastrophic backtracking risk. Anchoring at start and end ensures full string validation. For very large inputs, consider pre-filtering with simpler patterns to reduce 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 regex validate real calendar dates like February 29th in a leap year?

No. The regex only checks the format and numeric ranges (e.g., 02-29 is allowed even in non-leap years like 2023). Additional validation logic is required for real calendar correctness.

Why use non-capturing groups (?:...) instead of capturing groups (...)?

Non-capturing groups improve performance by avoiding unnecessary capture overhead since the groups are only used for alternation or grouping, not for extracting substrings.

Can this regex match times with timezone offsets like +0530 (without colon)?

No. The regex strictly requires the timezone offset to be in the format ±HH:MM with a colon separating hours and minutes.

Is the fractional seconds part mandatory?

No. The fractional seconds part is optional, as indicated by the trailing ? quantifier on the group.