Code
JWT (JSON Web Token) shape Regex
A JSON Web Token has three Base64url parts separated by dots: a header, a payload and a signature. Because the header and payload are JSON objects that begin with an opening brace and quote, their encoded form starts with eyJ. This pattern checks that structure and allows an empty signature, which appears with the insecure alg none setting. It is useful for spotting tokens in logs before they leak, for routing Authorization headers and for test assertions. It checks the shape only. It does not decode the parts, check the expiry or verify the signature, so never trust a token because it matches.
Pattern (JavaScript)
/^eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/Starts at the start of the line/string, then the text "eyJ", then one of the characters A-Za-z0-9_-, one or more times, then the text ".eyJ", then one of the characters A-Za-z0-9_-, one or more times, then the character ".", then one of the characters A-Za-z0-9_-, zero or more times, then the end of the line/string.
Limit: Checks the shape only, not the signature, expiry or claims.
Examples
Matches
- ✓ eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.c2lnbmF0dXJl
- ✓ eyJhbGciOiJub25lIn0.eyJ1c2VyIjoiQWxpIFRlc3QifQ.
- ✓ eyJ0.eyJ0.abc
Does not match
- ✗ abc.def.ghi
- ✗ eyJhbGci.eyJzdWIi
- ✗ eyJ a.eyJb.c
- ✗ eyJhbGci.eyJzdWIi.sig.extra
Try it
0 matches (JavaScript engine)
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.c2lnbmF0dXJl
eyJhbGciOiJub25lIn0.eyJ1c2VyIjoiQWxpIFRlc3QifQ.
eyJ0.eyJ0.abc
abc.def.ghi
eyJhbGci.eyJzdWIi
eyJ a.eyJb.c
eyJhbGci.eyJzdWIi.sig.extraCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/g;
const text = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.c2lnbmF0dXJl";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$')
text = 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.c2lnbmF0dXJl'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/';
$text = 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.c2lnbmF0dXJl';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^eyJ[A-Za-z0-9_-]+\\.eyJ[A-Za-z0-9_-]+\\.[A-Za-z0-9_-]*$");
String text = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.c2lnbmF0dXJl";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$`)
text := "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.c2lnbmF0dXJl"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$");
var text = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.c2lnbmF0dXJl";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$").unwrap();
let text = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.c2lnbmF0dXJl";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/
text = 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.c2lnbmF0dXJl'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why do JWTs start with eyJ?
- The header is a JSON object, so it begins with the characters {". Base64url-encoding those two characters always produces eyJ, which makes the prefix a reliable marker.
- Does a match mean the token is valid?
- No. The pattern cannot verify the signature, check the exp claim or confirm the issuer. Always verify tokens with a JWT library and the correct key on the server.
- Can I use this to redact tokens from logs?
- Yes. Remove the anchors, add the g flag and replace each match with a placeholder. Scanning logs for this shape is a cheap way to catch accidentally logged credentials.