GitHub Personal Access Token
Match GitHub Personal Access Tokens (classic + fine-grained) and OAuth tokens by their `ghX_` prefix.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("gh[pousr]_[A-Za-z0-9]{36,255}", "g");
const input = "Use ghp_AbCd1234EfGh5678IjKl9012MnOp3456QrSt for the API";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"gh[pousr]_[A-Za-z0-9]{36,255}")
input_text = "Use ghp_AbCd1234EfGh5678IjKl9012MnOp3456QrSt for the API"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`gh[pousr]_[A-Za-z0-9]{36,255}`)
input := `Use ghp_AbCd1234EfGh5678IjKl9012MnOp3456QrSt for the API`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
gh[pousr]_[A-Za-z0-9]{36,255} (flags: g)Raw source: gh[pousr]_[A-Za-z0-9]{36,255}
How it works
Examples
Input
Use ghp_AbCd1234EfGh5678IjKl9012MnOp3456QrSt for the APIMatches
ghp_AbCd1234EfGh5678IjKl9012MnOp3456QrSt
Input
GITHUB_TOKEN=gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMatches
gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Input
no token hereNo match
—Common use cases
- •Pre-commit secret-scanning hooks
- •PR diff scanning in CI
- •Backup / log redaction
- •Incident response after a leak
Related patterns
AWS Access Key ID
SecurityMatch AWS access key IDs (both long-term AKIA and temporary ASIA prefixes).
Generic API Key
SecurityMatch generic long alphanumeric tokens (32+ chars) typical of API keys and access tokens.
JWT Token
SecurityMatch JSON Web Tokens (JWTs) — three base64url-encoded segments separated by dots.
Bearer Token (Authorization Header)
SecurityMatch Bearer token values from HTTP Authorization headers, capturing the raw token string.