Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to simplify the username regular expression, if possible #1985

Closed
3 tasks
brylie opened this issue Dec 21, 2016 · 4 comments
Closed
3 tasks

Try to simplify the username regular expression, if possible #1985

brylie opened this issue Dec 21, 2016 · 4 comments
Assignees

Comments

@brylie
Copy link
Contributor

brylie commented Dec 21, 2016

Context

Pull request #1574 made changes to the username regular expression.

Before

// Username must be 3-15 alphanumeric string with hyphens allowed.
const UsernameRegEx = /^[a-z0-9A-Z_\-]{3,15}$/;

After

// Username must be 3-15 alphanumeric string with hyphens allowed.
const UsernameRegEx = /^(?!\d)(?!.*-.*-)(?!.*-$)(?!-)[a-zA-Z0-9-]{3,15}$/;

^ - Match at the beginning
(?!\d) - Do not match if the string starts with a digit
(?!.*-.*-) - Do not match if the string has 2 hyphens anywhere
(?!.*-$) - Do not match if the string ends with a hyphen
(?!-) - Do not match if the string starts with a hyphen
[a-zA-Z0-9-_]{3,15} - Match 3 to 15 characters from the range specified (username can have 1 hyphen and under_score with chars and numbers in it)
$ - Assert the end of string.

Issue

We have recently modified the reqular expression, and had trouble understanding its purpose. The comment does not describe the regular expression.

Task

  • Determine how the regular expression works as currently written.
  • Add comments describing why the regular expression is written as it is.
  • If possible, simplify the regular expression.
@brylie
Copy link
Contributor Author

brylie commented Dec 21, 2016

@NNN & @jykae, why did we change the username RegEx in pull request #1574? What does the current regex do, in plain language?

/cc @bajiat

@brylie
Copy link
Contributor Author

brylie commented Dec 21, 2016

It seems like the regular expression was designed to match the rules of Github usernames:

screenshot_20161221_135614

Since we are not syncing usernames upstream to Github, I am not sure that we need as strict of username rules.

@brylie
Copy link
Contributor Author

brylie commented Dec 21, 2016

Also, Github usernames can start with numbers, so we don't need to prevent our usernames from starting with numbers, in any case:

// We can probably remove this from our regex
/^(?!\d)

@brylie
Copy link
Contributor Author

brylie commented Dec 21, 2016

Also, Github usernames can have multiple hyphens, just not together. So, the following can be removed or modified:

// Github usernames can have multiple hyphens, just not together
(?!.*-.*-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants