Social
GitHub username Regex
GitHub usernames can be up to 39 characters and may contain letters, digits and hyphens. A hyphen cannot start or end the name, and two hyphens cannot appear in a row. This pattern enforces those rules with a lookahead for the total length and a body that only allows a hyphen when a letter or digit follows. Use it in developer portfolios, contributor forms and tooling that builds GitHub links from user input. Underscores are rejected because GitHub does not allow them. A match only means the name is well-formed, not that the account exists or is available to register.
Pattern (JavaScript)
/^(?=.{1,39}$)[A-Za-z0-9](?:-?[A-Za-z0-9])*$/Starts at the start of the line/string, then a position followed by (any character, between 1 and 39 times, then the end of the line/string), then a letter or digit, then (the character "-", optionally (zero or one time), then a letter or digit), zero or more times, then the end of the line/string.
Examples
Matches
- ✓ test-user
- ✓ octo-test-42
- ✓ a
- ✓ SampleDev
Does not match
- ✗ -testuser
- ✗ test--user
- ✗ testuser-
- ✗ test_user
Try it
0 matches (JavaScript engine)
test-user
octo-test-42
a
SampleDev
-testuser
test--user
testuser-
test_userCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?=.{1,39}$)[A-Za-z0-9](?:-?[A-Za-z0-9])*$/g;
const text = "test-user";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?=.{1,39}$)[A-Za-z0-9](?:-?[A-Za-z0-9])*$')
text = 'test-user'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?=.{1,39}$)[A-Za-z0-9](?:-?[A-Za-z0-9])*$/';
$text = 'test-user';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?=.{1,39}$)[A-Za-z0-9](?:-?[A-Za-z0-9])*$");
String text = "test-user";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
ℹ RE2 has no lookahead, so the 39 character cap is approximate; names with hyphens may exceed 39 characters. Check the length in code.
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}$`)
text := "test-user"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?=.{1,39}$)[A-Za-z0-9](?:-?[A-Za-z0-9])*$");
var text = "test-user";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
ℹ The regex crate has no lookahead, so the 39 character cap is approximate; names with hyphens may exceed 39 characters. Check the length in code.
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}$").unwrap();
let text = "test-user";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?=.{1,39}$)[A-Za-z0-9](?:-?[A-Za-z0-9])*$/
text = 'test-user'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are underscores rejected?
- GitHub usernames only allow alphanumeric characters and single hyphens. A name like test_user cannot be registered, so rejecting it early saves a confusing broken link later.
- How does the pattern block double hyphens?
- Each repetition of (?:-?[A-Za-z0-9]) adds at most one hyphen and must then add a letter or digit. Two hyphens in a row would need a repetition with no letter or digit, which is impossible.
- Does this also work for GitHub organisation names?
- Yes. Organisations follow the same naming rules as personal accounts, so the same pattern applies. Both share one namespace on GitHub.