Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^eyJ[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$

Matches JWT-like strings starting with 'eyJ' followed by two segments separated by dots, where each segment consists of alphanumeric characters, hyphens, or underscores. The third segment is optional.

Token-by-token breakdown

TokenMeaningExample
^ 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.ghi789
  • eyJ-_.def456_
  • eyJ123.456.
  • eyJabc.def

Does not match

  • eyJabc.def.ghi.jkl
  • eyJabc
  • eyJabc.def.
  • eyJabc..def
  • eyJabc.def.ghi!
  • eyJabc.def.ghi
  • eyJabc.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.

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 pattern validate JWTs?

No, it only checks the format of a string that resembles a JWT (three dot-separated segments with allowed characters). It does not validate the content, signature, or structure of a JWT.

Why are the dots escaped (\.)?

In regex, a dot (.) is a metacharacter that matches any character. To match a literal dot, it must be escaped as \., otherwise the pattern would match any character between segments.

Can the third segment be empty?

Yes, the third segment is optional due to the * quantifier (zero or more). Examples like 'eyJabc.def456_' are valid matches.

Is this pattern safe for user input validation?

It depends on the use case. While it enforces a basic format, it does not validate the content or security of the JWT. Additional checks (e.g., decoding, signature validation) are recommended for JWTs.