github-username-regex

A regular expression that only matches a valid Github username

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
github-username-regex
1.0.06 years ago6 years agoMinified + gzip package size for github-username-regex in KB

Readme

github-username-regex
NPM version Bower version Build Status
A regular expression that only matches a valid Github username
import githubUsernameRegex from 'github-username-regex';

githubUsernameRegex.test('john'); //=> true
githubUsernameRegex.test('john-due'); //=> true
githubUsernameRegex.test('john-due-'); //=> false

According to the form validation messages on Join Github page,
  • Github username may only contain alphanumeric characters or hyphens.
  • Github username cannot have multiple consecutive hyphens.
  • Github username cannot begin or end with a hyphen.
  • Maximum is 39 characters.

Installation

npm

npm install github-username-regex

bower

bower install github-username-regex

API

import githubUsernameRegex from 'github-username-regex';

githubUsernameRegex

Type: RegExp (/^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$/i)
// Returns `true`
githubUsernameRegex.test('a');
githubUsernameRegex.test('0');
githubUsernameRegex.test('a-b');
githubUsernameRegex.test('a-b-123');
githubUsernameRegex.test('a'.repeat(39));

// Returns `false`
githubUsernameRegex.test('');
githubUsernameRegex.test('a_b');
githubUsernameRegex.test('a--b');
githubUsernameRegex.test('a-b-');
githubUsernameRegex.test('-a-b');
githubUsernameRegex.test('a'.repeat(40));

Note that this module doesn't take reserved usernames into consideration. For example it matches help, about and pricing, though they are reserved words and cannot be used as Github usernames.

License

Creative Commons Zero v1.0 Universal