github-username-regex-js
v1.0.0
Published
A regular expression that only matches a valid GitHub or GitHub EMU username
Downloads
132
Maintainers
Readme
github-username-regex-js ✅
A regular expression that only matches a valid GitHub username or GitHub EMU username.
import githubUsernameRegex from 'github-username-regex-js';
githubUsernameRegex.test('monalisa'); //=> true
githubUsernameRegex.test('mona-lisa'); //=> true
githubUsernameRegex.test('name_company'); //=> true - GitHub EMU
githubUsernameRegex.test('mona-lisa-'); //=> false
githubUsernameRegex.test('mona%$^lisa'); //=> false
According to the form validation messages on the Join GitHub page,
- GitHub usernames may only contain alphanumeric characters or hyphens
- GitHub usernames cannot have multiple consecutive hyphens
- GitHub usernames cannot begin or end with a hyphen
- Usernames can have a maximum of 39 characters
Additionally, this package (regex) also checks for GitHub EMU handles to see if they are valid as well. GitHub EMUs can look like this:
name_company
name-with-hyphens_company
name-with-numbers123_company123
- etc
Installation
You can install this package via npm:
npm install github-username-regex-js
Usage
// Import the package
import githubUsernameRegex from 'github-username-regex-js';
// Returns `true`
githubUsernameRegex.test('a');
githubUsernameRegex.test('0');
githubUsernameRegex.test('a-b');
githubUsernameRegex.test('a-b-123');
githubUsernameRegex.test('a'.repeat(39));
githubUsernameRegex.test('some-person_githubEmuCompany'); // GitHub EMU
// 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] This module doesn't take reserved usernames into consideration. For example, it matches
help
,about
andpricing
even though they are reserved words and cannot be used as GitHub usernames.