Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts the position at the start of the string | Start of 'eyJ...' |
eyJ |
Literal characters 'e', 'y', 'J' | eyJ in 'eyJ...' |
[A-Za-z0-9-_]+ |
Character class matching one or more alphanumeric characters, hyphens, or underscores | abc123- in 'eyJabc123-...' |
\. |
Escaped literal dot (.) | . |
[A-Za-z0-9-_]+ |
Character class matching one or more alphanumeric characters, hyphens, or underscores | def456_ in 'eyJabc123-.def456_...' |
\. |
Escaped literal dot (.) | . |
[A-Za-z0-9-_]* |
Character class matching zero or more alphanumeric characters, hyphens, or underscores | ghi789 or empty in 'eyJabc123-.def456_.ghi789' or 'eyJabc123-.def456_' |
$ |
Anchor: asserts the position at the end of the string | End of '...ghi789' |
Matches
eyJabc123.def456.ghi789eyJ-_.def456_eyJ123.456.eyJabc.def
Does not match
eyJabc.def.ghi.jkleyJabceyJabc.def.eyJabc..defeyJabc.def.ghi!eyJabc.def.ghieyJabc.def.ghi
Common pitfalls
- Assuming the pattern matches valid JWTs (it only checks the format, not the content or structure).
- Forgetting to escape the dots (\.), which would make the regex match any character instead of a literal dot.
- Not anchoring the pattern (missing ^ or $), which could lead to partial matches in longer strings.
- Overlooking that the third segment is optional (using * instead of +), which might not be intended.
Performance notes
The pattern is efficient as it uses simple character classes and quantifiers without nested quantifiers or backtracking risks. The anchors (^ and $) ensure the regex matches the entire string, preventing partial matches.