Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
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:45Z2023-12-31T23:59:59.999999+05:301999-01-01T00:00:00-08:002000-02-29T12:00:00.123456Z
Does not match
2023-13-01T12:00:00Z2023-02-30T12:00:00Z2023-05-07T24:00:00Z2023-05-07T14:60:00Z2023-05-07T14:30:60Z2023-05-07T14:30:45.1234567Z2023-05-07T14:30:45+24:002023-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.