Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex for JSON Web Token (JWT)
Regex pattern · validation

Regex for JSON Web Token (JWT)

Regex for JWT token: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/ as a JSON Web Token (JWT) regex. It matches "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjMifQ.dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk" and "eyJhbGciOiJub25lIn0.eyJzdWIiOiIxIn0.", and rejects "abc.def" and "a.b.c.d". Matching the shape says nothing about validity.

Last updated · Maintained by FreeAIToolz · How we test

/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/
  • eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjMifQ.dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXkmatches
  • eyJhbGciOiJub25lIn0.eyJzdWIiOiIxIn0.matches
  • abc.defno match
  • a.b.c.dno match
  • abc.d=ef.ghino match
  • abc def.ghi.jklno match
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
[A-Za-z0-9_-]+base64url header
\.dot
[A-Za-z0-9_-]+base64url payload
\.[A-Za-z0-9_-]*$signature (empty for unsigned tokens)

Examples

InputResult
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjMifQ.dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk✓ Matches
eyJhbGciOiJub25lIn0.eyJzdWIiOiIxIn0.✓ Matches
abc.def✗ No match
a.b.c.d✗ No match
abc.d=ef.ghi✗ No match
abc def.ghi.jkl✗ No match

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

Matching the shape says nothing about validity. Always verify the signature, expiry and audience with a JWT library, and reject alg "none" tokens in production.

When to use this pattern

Detecting bearer tokens in headers or logs (for redaction) before decoding.

Use it in your language

JavaScript

const re = /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/;
re.test(input); // true or false

Python

import re
pattern = re.compile(r'^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$')
bool(pattern.search(value))

PHP

$ok = preg_match('~^[A-Za-z0-9_-]+\\.[A-Za-z0-9_-]+\\.[A-Za-z0-9_-]*$~', $value) === 1;

Java

Pattern pattern = Pattern.compile("^[A-Za-z0-9_-]+\\.[A-Za-z0-9_-]+\\.[A-Za-z0-9_-]*$");
boolean ok = pattern.matcher(value).find();

Go

re := regexp.MustCompile(`^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$`)
ok := re.MatchString(value)

More regex patterns

Frequently asked questions

What is the regex for JSON Web Token (JWT)?
/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/. Matching the shape says nothing about validity. Always verify the signature, expiry and audience with a JWT library, and reject alg "none" tokens in production.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
Why does the pattern start with ^ and end with $?
They anchor the match to the whole string, so "abceyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjMifQ.dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXkxyz" cannot pass just because it contains a valid part. To search for matches inside longer text, remove ^ and $ (use \b where word boundaries help) and add the g flag.