^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$ This pattern checks the structural shape of a JSON Web Token: three Base64URL-encoded segments (header, payload, signature) separated by dots. It confirms the format only — it does not decode or verify the token. Security-oriented patterns check sensitive or policy-bound input: password composition, card number shapes, tokens, private IP ranges and national identifiers. They are a first line of defense, never the whole story — pair them with checksums, real verification, encryption and access control. A regex can confirm a format but cannot prove a secret is valid or safe. It needs no special flags. Apply it to the whole input as a format contract. Base64URL omits padding =, so this pattern rejects padded Base64; JWTs are unpadded by spec. A matching string may still be malformed JSON inside or have an invalid signature — decode and verify with a JWT library. Never trust a token because it matches a regex; signature verification and claim checks (exp, iss, aud) are mandatory. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.
Token-by-token explanation
[A-Za-z0-9-_]+ — Base64URL alphabet (letters, digits, hyphen, underscore)\. — literal dot separating segmentsthree groups — header.payload.signature^...$ — anchors ensuring the whole string is the token
What it matches
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0In0.abc123-_signatureaaa.bbb.ccc What it does not match
aaa.bbbaaa.bbb.ccc.dddeyJ...==.x.ynot a jwt When to use it
Reach for a security pattern as one layer of input handling for sensitive or policy-bound data: password rules, card shapes, tokens, private ranges and national identifiers. Never treat a match as proof of validity or safety — combine it with checksums, signature verification, breach screening, encryption and strict access control. Also guard against ReDoS by avoiding nested quantifiers and bounding input length, since security checks are a tempting target for denial-of-service abuse.
Caveats
- Base64URL omits padding =, so this pattern rejects padded Base64; JWTs are unpadded by spec.
- A matching string may still be malformed JSON inside or have an invalid signature — decode and verify with a JWT library.
- Never trust a token because it matches a regex; signature verification and claim checks (exp, iss, aud) are mandatory.
Code examples
const re = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/;
const input = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0In0.abc123-_signature";
if (re.test(input)) {
// valid: proceed
} else {
// reject with a helpful message
} import re
rx = re.compile(r"^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$")
value = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0In0.abc123-_signature"
if rx.fullmatch(value):
pass # valid
else:
raise ValueError("invalid format") import java.util.regex.*;
Pattern p = Pattern.compile("^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+$");
Matcher m = p.matcher("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0In0.abc123-_signature");
boolean ok = m.matches(); // whole input must conform <?php
$subject = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0In0.abc123-_signature";
if (preg_match('/^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/', $subject, $m)) {
// $m[0] is the full match
} How to test it
Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.
Frequently asked questions
Why three segments?
A JWS JWT is header.payload.signature, each Base64URL-encoded. The header declares the algorithm, the payload carries claims, and the signature protects integrity.
Does this verify the token?
No — it only checks the three-dot Base64URL shape. Use a library to decode, verify the signature against the key, and validate expiration and audience claims.
Is a regex enough to secure sensitive input?
Never on its own. Regex checks format; security also needs checksums (Luhn), real verification (token signatures, breach screening), encryption, masking and access control. Treat the pattern as one layer, not the solution.
How do I prevent ReDoS in security checks?
Avoid nested unbounded quantifiers, set a match timeout where the engine supports it, cap input length, and prefer simple linear patterns. Test suspicious inputs with long non-matching strings before deploying.
Try the Regex Tester
Live regex matching with groups, flags and explanations.