Regular Expressions 101

Community Patterns

JWT pattern

1

Regular Expression
ECMAScript (JavaScript)

/
eyJ[A-Za-z0-9-_]+\.(?:eyJ[A-Za-z0-9-_]+)?\.[A-Za-z0-9-_]{2,}(?:(?:\.[A-Za-z0-9-_]{2,}){2})?
/
g

Description

  • handles both JWS (3 segments) and JWE (5 segments)
    • remove the ending (?:(?:... to handle JWS only
  • payload (2nd segment) may be empty
  • both header (1st segment) and payload are expected to start with eyJ, a base64 encoded start of JSON object {"
  • accepts only proper Base64url encoding (RFC 4648) without Base64 padding =
Submitted by iki - 24 days ago