[Regex] Need help with CheckUsername, Test Failed

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.

  1. 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.
  2. 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:

Here we need to test for two things :slight_smile:

  1. One is that the if the alphabet is just 1, the numeric characters should be two or more, thus making the whole string three or more characters.
    OR
  2. Second thing could be that we have two or more alphabetic characters. If the string is just two chars, it should be just alphabetic. So then we have room for 0 more numeric characters, thus making the whole string two or more characters.

So we need to use the OR or alternation operator we learned in regex to handle these two cases separately.

1 Like

Remember to also include [A-Za-z] coz we need match lowercase alphabets.
So [A-Za-z] should be at the beginning in either case. You used ^ for that which is right. The digits are also correct but could be specified with the regex \d too. You got digits at the end with $ which is correct. Now only thing left is to adjust the occurrences of both the things - [A-Za-z] and \d in both the cases separated by |. I am giving you just hints instead of the solution coz you are just there.

The lookahead expression tests the string in general, it doesn’t restrict the test to just the beginning of the string. Review the Match Beginning String Patterns challenge to see how to anchor a regex to the beginning of a string.

Thank you for giving me a hint. Finally I solved the test !

Here’s my final solution :

let userCheck = /(?<=^[A-Za-z]{1,2})([A-Za-z]|(\d(?=\d)))+\d*$/;

  1. (?<=^[A-Za-z]{1,2}) to match minimum 1, maximum 2 alphabet in the beginning
  2. [A-Za-z] match the alphabet after minimum 1 and maximum 2 alphabet OR \d match a numeric character after minimum 1 and maximum 2 alphabet in the beginning ONLY IF followed by another numeric character
  3. + quantifier to match the expression in point 2, 1 or more times.
  4. And finally \d*$ to match number in the end of the input, using * quantifier because the number occur in the end of the input can occur zero times or more.

Edit : Feel free to review my regex :smile:

Finally I pass all of the test. Here’s my final regex expression :

let userCheck = /(?<=^[A-Za-z]{1,2})([A-Za-z]|(\d(?=\d)))+\d*$/;

I move the anchor ^ to inside the lookbehind assertion

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.