JWT Token in GO
Match JSON Web Tokens (JWTs) — three base64url-encoded segments separated by dots.
Try it in the GO tester →Pattern
regexGO
eyJ[A-Za-z0-9_\-]+\.eyJ[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+ (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`eyJ[A-Za-z0-9_\-]+\.eyJ[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+`)
input := `eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.abc123-_`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
How the pattern works
Both header and payload segments start with eyJ (base64 of '{"'), followed by base64url chars, joined by dots and followed by the signature segment.
Examples
Input
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.abc123-_Matches
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.abc123-_
Input
Bearer token-hereNo match
—