update restrict possible usernames solution (#35973)

pull/36009/head
Tom 2019-05-05 15:37:03 -05:00 committed by Randell Dawson
parent a24ecfbd85
commit 3cecf26f44
1 changed files with 9 additions and 15 deletions

View File

@ -6,21 +6,15 @@ title: Restrict Possible Usernames
## Solution:
```javascript
let username = "JackOfAllTrades";
let userCheck = /^[a-z]{2,}\d*$/i;
const userCheck = /^[a-z]([0-9]{2,}|[a-z]+\d*)$/i;
let result = userCheck.test(username);
```
## Explain:
1. The only numbers in the username have to be at the end. `\d$`
There can be zero or more of them at the end. `*`
```javascript
/\d*$/;
```
2. Username letters can be lowercase and uppercase. `i`
```javascript
/\d*$/i;
```
3. Usernames have to be at least two characters long. `{2,}`
A two-letter username can only use alphabet letter characters. `^[a-z]`
```javascript
/^[a-z]{2,}\d*$/i;
```
1. `^` - start of input
2. `[a-z]` - first character is a letter
3. `[0-9]{2,0}` - ends with two or more numbers
4. `|` - or
5. `[a-z]+` - has one or more letters next
6. `\d*` - and ends with zero or more numbers
7. `$` - end of input
8. `i` - ignore case of input