Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | 2023-01-01T12:34:56Z |
\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) | 34 |
: |
Literal colon character | : |
\d{2} |
Exactly 2 digits (seconds) | 56 |
(?:\.\d{1,6})? |
Non-capturing group: optional fractional seconds (1 to 6 digits) preceded by a dot | .123456 |
(?:Z|[+-]\d{2}:\d{2}) |
Non-capturing group: either 'Z' for UTC or timezone offset in ±HH:MM format | Z |
$ |
Anchor: asserts position at end of the string | 2023-01-01T12:34:56Z |
Matches
2023-01-01T12:34:56Z2023-12-31T23:59:59.999999+05:301999-02-28T00:00:00-08:002000-01-01T00:00:00.1Z
Does not match
2023/01/01T12:34:56Z2023-1-1T12:34:56Z2023-01-01 12:34:56Z2023-01-01T12:34:562023-01-01T12:34:56.1234567Z2023-01-01T25:00:00Z2023-01-01T12:60:00Z
Common pitfalls
- Forgetting the 'T' separator between date and time
- Using single-digit months or days without leading zeros
- Including more than 6 fractional seconds digits
- Omitting the timezone entirely
- Using invalid timezone offsets (e.g., +25:00)
- Assuming this matches all ISO 8601 formats (e.g., it does not match formats with space instead of 'T')
Performance notes
The pattern is linear and does not contain nested quantifiers, so catastrophic backtracking is not a concern. Anchors (^ and $) ensure the entire string is matched, improving efficiency. For very long strings, the anchors prevent partial matches.