JavaScript / ECMAScript

GitHub Personal Access Token in JS

Match GitHub Personal Access Tokens (classic + fine-grained) and OAuth tokens by their `ghX_` prefix.

Try it in the JS tester →

Pattern

regexJS
gh[pousr]_[A-Za-z0-9]{36,255}   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
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).

How the pattern works

gh[pousr]_ matches the GitHub token-type prefix: ghp_ (PAT classic), gho_ (OAuth), ghu_ (user-to-server), ghs_ (server-to-server), ghr_ (refresh). [A-Za-z0-9]{36,255} matches the token body — GitHub fine-grained tokens are longer than the classic 36-char tokens, so we allow up to 255.

Examples

Input

Use ghp_AbCd1234EfGh5678IjKl9012MnOp3456QrSt for the API

Matches

  • ghp_AbCd1234EfGh5678IjKl9012MnOp3456QrSt

Input

GITHUB_TOKEN=gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Matches

  • gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Input

no token here

No match

Same pattern, other engines

← Back to GitHub Personal Access Token overview (all engines)