your regex is good but the way ‘test’ works is weird…
I changed your code to:
let username = “JackOfAllTrades”;
let userCheck = /^[a-z]+[a-z]+\d*$/i; // Change this line
let result = username.match(userCheck);
and that worked for me
(changed test to match and removed the global match)
edit: i think the reason that ‘g’ makes things not work is because with each successive call to match or test, the command tries to find the next match (starting not from the beginning of the string but from where the end of the last match was…)
Thank you! About the g, then if I use g, I can only have “one condition”? For example:
/[a-e]/g would search for a, b, c,d,e from the beginning to the end of the string, but if I had /[a-e][2]/g, it would only take account the first section because before the second section starts, the string would be over?
i’m sorry I can’t answer this. I still don’t really know why your original regex was not working.
The only case that your original regex fails is when you get a string like ‘s39’ which should pass, but your regex fails to recognize it as valid. But that is not part of the test suite that FCC gives.
Except you have to account for them if they are part of the requirement. The strictness of regex depends on where you are using it for, but in general neglecting spec can lead you to unexpected bugs and security issues.
Even though these requirements are silly for a username, if this was used for validating username, then it is better to be strict about it.
If your regex was used with regex.test(), so many cases would be false positive. If it was used with regex.match(), it won’t capture enough characters even the valid ones.
So, it definitely can’t serve as a choke point to validate username.
i mean, it’s a typo, correct?
if two-letter was two-character i would agree with you, and your regex would work.
but my thoughts are:
it would be impossible for a two-letter username to have anything other than alphabet letter characters so i’m interpreting the rule as a technical parameter rather than an obvious clarification.
If that constraint existed independently, then it might be ambiguous but
Usernames have to be at least two characters long. A two-letter username can only use alphabet letter characters.
This sounds more or less like:
“Username must be at least two characters, but if a username is exactly two characters, then all the characters must be alphabets.”