Restrict Possible Usernames - Z97

Tell us what’s happening:

I successfully match all items except Z97.

To start, check for a match of 2 letters at the beginning. Then, look for a match of 0 or more numbers at the end of the regex.

Not sure how to allow numbers as a second character so long as the password is over 2 characters.

Your code so far

let username = "Ofd1";
let userCheck = /^[A-Za-z][A-Za-z]+[0-9]*$/; // Change this line
let result = userCheck.test(username);
console.log(userCheck.test(username));
console.log(username.match(userCheck));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.

Challenge: Restrict Possible Usernames

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/restrict-possible-usernames

^[A-Za-z][0-9]{2,}$|^[A-Za-z][A-Za-z]+[0-9]*$

Forgot about the OR operator.

Since the ^[A-Z-a-z] is repeated, see if you can use a non-capture group for the OR part. Also, instead of using [A-Z-a-z], think of a way you can use only [a-z].

1 Like

Ok, so the below works, but I have to say, I don’t really understand the non-capture group and can’t find a good explanation of it anywhere. This took some tinkering, and I’m still using OR.


let username = "faa";

let userCheck = /(?:^[a-z]{2,}|^[a-z][0-9]{2,})+[0-9]*$/i; // Change this line

let result = userCheck.test(username);

console.log(userCheck.test(username));

console.log(username.match(userCheck));