Aarunya AppsAarunya Apps

GitHub UsernameRegex — Pattern, Examples & Code

^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,37}[a-zA-Z0-9])?$

What it matches / doesn't match

Matches ✅Does not match ❌
octocat-leading
my-usernametrailing-
user123this-username-is-way-too-long-for-github-policy

How it works

Follows GitHub's own username rules: 1–39 characters, only letters, digits, and hyphens, and cannot start or end with a hyphen. Single-character usernames are also valid (the optional group handles the length 2–39 case). Consecutive hyphens are technically allowed by GitHub but uncommon.

Code Usage

JavaScript

const regex = /^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,37}[a-zA-Z0-9])?$/;
regex.test("octocat"); // true

Python

import re
pattern = re.compile(r'^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,37}[a-zA-Z0-9])?$')
bool(pattern.match("octocat"))  # True

Go

import "regexp"
r := regexp.MustCompile(`^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,37}[a-zA-Z0-9])?$`)
r.MatchString("octocat") // true

Need a custom regex for a different use case?

Generate a custom regex with AI →

FAQ

What does the GitHub Username regex pattern match?

Follows GitHub's own username rules: 1–39 characters, only letters, digits, and hyphens, and cannot start or end with a hyphen. Single-character usernames are also valid (the optional group handles the length 2–39 case). Consecutive hyphens are technically allowed by GitHub but uncommon.

How do I use the GitHub Username regex in JavaScript?

const regex = /^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,37}[a-zA-Z0-9])?$/; regex.test("octocat"); // true

Can I use the GitHub Username regex in Python?

Yes. import re pattern = re.compile(r'^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,37}[a-zA-Z0-9])?$') bool(pattern.match("octocat")) # True