Tell us what’s happening:
I failed in ’ Your regex should match the string Z97
’ test.
Your code so far
let username = "JackOfAllTrades";
let userCheck = /(?=[A-Z]{1,})(^[A-Z]+(?<=[A-Z]{2,})[0-9]*$)/i; // Change this line
let result = userCheck.test(username);
Here’s the explanation of my code, I’m still learning about regex, please correct me if there is something wrong in my explanation, and a hint will be much appreciated.
- I check the beginning of the string with
(?=[A-Z]{1,})
, the expression is to match first character, Because at least the two character in the beginning has to be letter. - And this code :
(^[A-Z]+(?<=[A-Z]{2,})[0-9]*$)
Is to make the expression only match with letter in the beginning, if the expression in poin no. 1 is not matched. The +
quantifier is to match [A-Z] 1 or more times, {2,}
to match minimum 2 occurences. [0-9]*$
is to match [0-9] zero or more times, $
assertions is for matches the end of the string, because numbers have to be at the end.
I know I didn’t pass ’ Your regex should match the string Z97
’ test because, I think ?<=[A-Z]{2,}
expression only match minimum two alphabet letter in the beginning. But if I change ?<=[A-Z]{2,}
into ?<=[A-Z]{1,}
, I got this result :
// running tests Your regex should not match the string
J
Your regex should not match the string
A1
// tests completed
So my question is, based on the output above, in order to pass the test, is it possible to add ‘conditional’ to *
quantifier? Like 'if the first character is a letter and it not followed by anything, so the *
quantifier doesn’t apply. Also, how to match 'if the string is two characters long, so the 1st and the 2nd character is have to be a alphabet letter? Any hint is appreciated. Thank you.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0
Challenge: Restrict Possible Usernames
Link to the challenge: