Skip to main content
Regexflux

GitHub Username

intermediate

Matches valid GitHub usernames.

socialgithubusernamevalidation

Pattern

/[a-zA-Z\d](?:[a-zA-Z\d]|-(?=[a-zA-Z\d])){0,38}/g

Try It

0 of 6 strings matched

Explanation

Matches GitHub usernames: starts with alphanumeric, allows hyphens (but not consecutive or trailing), max 39 characters. Uses a lookahead to prevent trailing hyphens.

Test Strings

Matching

  • octocat
  • user-name
  • a1b2c3

Non-matching

  • ---
  • ...

Language Compatibility

LanguageSupport
JSFull support
PYTHONFull support
JAVAFull support
PHPFull support
GONot supported (RE2 engine)

Code Snippets

const regex = /[a-zA-Z\d](?:[a-zA-Z\d]|-(?=[a-zA-Z\d])){0,38}/g;
const text = "your text here";
const matches = text.match(regex);
console.log(matches);

Related Patterns