https://github.com/grantbirki/github-username-regex-js
A regular expression that only matches a currently valid Github username or GitHub EMU handle
https://github.com/grantbirki/github-username-regex-js
github nodejs regex validation
Last synced: 6 months ago
JSON representation
A regular expression that only matches a currently valid Github username or GitHub EMU handle
- Host: GitHub
- URL: https://github.com/grantbirki/github-username-regex-js
- Owner: GrantBirki
- License: cc0-1.0
- Created: 2023-12-01T20:29:56.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-23T22:25:55.000Z (almost 2 years ago)
- Last Synced: 2024-12-09T14:18:53.495Z (over 1 year ago)
- Topics: github, nodejs, regex, validation
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/github-username-regex-js
- Size: 129 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# github-username-regex-js ✅
[](https://www.npmjs.com/package/github-username-regex-js) [](https://github.com/GrantBirki/github-username-regex-js/actions/workflows/lint.yml) [](https://github.com/GrantBirki/github-username-regex-js/actions/workflows/test.yml) [](https://github.com/GrantBirki/github-username-regex-js/actions/workflows/build.yml)
A regular expression that only matches a valid [GitHub](https://github.com/) username or [GitHub EMU](https://docs.github.com/en/enterprise-cloud@latest/admin/identity-and-access-management/understanding-iam-for-enterprises/about-enterprise-managed-users) username.
```javascript
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*](https://github.com/join) 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](https://www.npmjs.com/package/github-username-regex-js) via [npm](https://www.npmjs.com/):
```bash
npm install github-username-regex-js
```
## Usage
```javascript
// 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` and `pricing` even though they are reserved words and cannot be used as GitHub usernames.
## License
[Creative Commons Zero v1.0 Universal](https://creativecommons.org/publicdomain/zero/1.0/deed)