Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:Z|[+-]\d{2}:?\d{2})$

Matches a full ISO 8601 date-time string in the format YYYY-MM-DDTHH:MM:SS followed by either 'Z' for UTC or a timezone offset like +05:30 or -0800.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of '2023-10-05T14:30:00Z'
\d Digit character class (0-9) Any digit like '2' in '2023'
{4} Quantifier: exactly 4 times Matches '2023' in '2023-10-05T14:30:00Z'
- Literal hyphen character The '-' in '2023-10-05T14:30:00Z'
\d Digit character class (0-9) Any digit like '1' in '10'
{2} Quantifier: exactly 2 times Matches '10' in '2023-10-05T14:30:00Z'
- Literal hyphen character The '-' in '2023-10-05T14:30:00Z'
\d Digit character class (0-9) Any digit like '0' in '05'
{2} Quantifier: exactly 2 times Matches '05' in '2023-10-05T14:30:00Z'
T Literal 'T' character separating date and time The 'T' in '2023-10-05T14:30:00Z'
\d Digit character class (0-9) Any digit like '1' in '14'
{2} Quantifier: exactly 2 times Matches '14' in '2023-10-05T14:30:00Z'
: Literal colon character The ':' in '14:30:00Z'
\d Digit character class (0-9) Any digit like '3' in '30'
{2} Quantifier: exactly 2 times Matches '30' in '2023-10-05T14:30:00Z'
: Literal colon character The ':' in '14:30:00Z'
\d Digit character class (0-9) Any digit like '0' in '00'
{2} Quantifier: exactly 2 times Matches '00' in '2023-10-05T14:30:00Z'
(?: Non-capturing group start (JavaScript flavor) Start of '(?:Z|[+-]\d{2}:?\d{2})'
Z Literal 'Z' character for UTC timezone The 'Z' in '2023-10-05T14:30:00Z'
| Alternation (OR operator) Separates 'Z' and '[+-]\d{2}:?\d{2}'
[+-] Character class matching '+' or '-' The '+' in '+05:30' or '-' in '-0800'
\d Digit character class (0-9) Any digit like '0' in '05'
{2} Quantifier: exactly 2 times Matches '05' in '+05:30'
: Optional literal colon character (due to :?) The ':' in '+05:30'
? Quantifier: 0 or 1 times (makes colon optional) Allows '+' in '+0530'
\d Digit character class (0-9) Any digit like '3' in '30'
{2} Quantifier: exactly 2 times Matches '30' in '+05:30'
) Non-capturing group end End of '(?:Z|[+-]\d{2}:?\d{2})'
$ Anchor: asserts position at end of the string End of '2023-10-05T14:30:00Z'

Matches

  • 2023-10-05T14:30:00Z
  • 1999-12-31T23:59:59+00:00
  • 2000-01-01T00:00:00-0800
  • 2025-06-15T12:45:30+0530

Does not match

  • 2023-10-5T14:30:00Z
  • 2023-10-05T14:30:00
  • 2023-10-05T14:30:00+25:00
  • 2023-10-05T14:30:00Z
  • 2023-10-05T14:30:00+05:3

Common pitfalls

  • Forgetting the 'T' separator between date and time
  • Incorrect timezone offsets (e.g., +25:00 is invalid)
  • Missing leading zeros in month/day/hour/minute/second (e.g., '2023-5-5T5:5:5Z')
  • Including spaces or other characters after the timezone
  • Using capturing groups instead of non-capturing groups for alternation

Performance notes

The pattern is linear in complexity with no nested quantifiers, so catastrophic backtracking is not a concern. Anchors (^ and $) ensure the entire string is validated, preventing partial matches. For very large inputs, ensure the regex is applied to the entire string rather than searching within it.

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 (...)?

Non-capturing groups are more efficient as they do not store the matched substring in memory, which is unnecessary for alternation in this pattern.

Does this pattern validate all valid ISO 8601 date-time strings?

No. It only matches the specific format YYYY-MM-DDTHH:MM:SS with UTC or timezone offsets in the form ±HH:MM or ±HHMM. It does not validate dates like February 30th or times like 25:00:00.

Can this pattern match timestamps with fractional seconds?

No. The pattern strictly requires seconds to be exactly two digits (SS) without any fractional part.