Restrict Possible Usernames: cant restrict number

Restrict Possible Usernames: cant restrict number
0

#1

Tell us what’s happening:
I dont know how to fulfill restricting the username to at least 2 or more characters. I dont remember fcc teaching that. The two parameters i am failing right now are:
Your regex should match JACK
Your regex should not match J
For some reason, when i remove the g flag, the JACK problem is suddenly taken care of. why?

Your code so far


let username = "JackOfAllTrades";
let userCheck = /\D\d*$/gi; // Change this line
let result = userCheck.test(username);

Your browser information:

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


#2

So firstly you’ve not got the right amount of alphabetical characters, and you’re including numbers for some reason

To explicitly get just alphabetical characters, you would use a range [a-zA-Z] (or just [a-z] or [A-Z] with the i flag) explicitly rather than just not a digit, and you want two of these so stick two of them in there

You’re right that the digits are optional at the end and can have zero or more, so your quantifier * is correct, but you know of other quantifiers too - you’ve seen + before, and I think at this point you’ve seen {n,m} too haven’t you?


About the difference here between having g and not, honestly not sure - however I suspect it has something to do with lastIndex being set (a property of the regular expression object) causing test to fail after it runs first on username and then on a shorter string during the tests.

If it is that then chalk it up to another case of questionable design decision (no doubt they wanted us to be able to do something like while(regex.test(string)) but meh

Edit: Adding a sentence I forgot about the other quantifiers


#4

The solution given in the “Ask for Hint” section is correct, but not according to what has been learnt on FCC.

This was my solution, and it passed the challenge:

let username = "JackOfAllTrades";
let userCheck = /^[a-z]+[a-z]+\d*$/i;
let result = userCheck.test(username);

While the more correct solution would be the code below, the previous challenges did not yet teach what the curly braces do.

let username = "JackOfAllTrades";
let userCheck = /^[a-z]{2,}\d*$/i; // Change this line
let result = userCheck.test(username);