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.
oh, youâre totally right.
i wasnât thinking there could be letters after the numbers or two character usernames with numbers.
will delete for clarity.
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.â