Skip to main content
AllDevToolsHub
Back to all patterns

GitHub Username

Validation

Validates a GitHub username per GitHub's documented rules.

/^[a-zA-Z0-9](?:[a-zA-Z0-9]|-(?=[a-zA-Z0-9])){0,38}$/

How it works

GitHub usernames are 1–39 characters, alphanumeric or hyphen, cannot start or end with a hyphen, and cannot have consecutive hyphens. The lookahead enforces no trailing or doubled hyphens.

Test Cases

Should Match

  • torvalds
  • octocat
  • a-b-c
  • User1

Should NOT Match

  • -leading
  • trailing-
  • double--hyphen

Quick Summary

Validates a GitHub username (1–39 chars, alphanumeric or single hyphens, no leading/trailing hyphen).