Restrict Possible Usernames(One Tests fails)

Tell us what’s happening:

I have written the Regex for the challenge Restrict Possible Usernames as below. When I run the regex and try to run all the tests on my own i.e. by printing them:

console.log(result)
console.log(result2)

but somehow the regex fails one test i.e. Your regex should not match J. I don’t know why that’s happening. So, Can anybody suggest me anything?

Your code so far


let username = "J";
// Usernames have to be at least two characters long.
// Username letters can be lowercase and uppercase.

let userCheck = /^[a-z][a-z]*[0-9]*$/i; // Change this line
let result = userCheck.test(username);

let result2 = username.match(userCheck)

console.log(result)
console.log(result2)

// /^(?=^abc)(?=.*xyz$)(?=.*123)(?=^(?:(?!456).)*$).*$/

// /(?=([a-z][a-z]*))(?=([0-9]$))/ig

// [0-9]$

// /^[a-z]{2,}\d*$/i

// /[a-z][a-z]*[0-9]$/ig


// Usernames have to be at least two characters long

Your browser information:

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

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

You use the regex character * after your second [a-z], which means zero or more letters after the first letter. If there are zero letters after the first letter, you are not making sure that username is at least two characters long and J would be valid, which is not what you intend. Use the regex character that makes sure there are ‘one or more’ letters instead of ‘zero or more’ after the first letter.