Username check help plz :)

I cant get my code to accept the Z97 username but it does accept and reject all others correctly, very new to coding this is the first course I have done so not even sure that I can use the ‘if’ statement within the regex or if I have satisfied the condition of 4. Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.
In the test it rejects A1 but if does so as well with just this code
let userCheck = /^[a-z]{2,}\d*$/i;
I am totally lost as to why it wont accept Z97 as a username and the solutions seem to just put in conditions to satisfy Z97 even though its not in the requirements for the code.
Very confused! Thanks for any help

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36.

Challenge: Restrict Possible Usernames

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

No, you can’t have a JS if statement inside the regex. You can have a logical operator and qualifiers like you already do.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

1 Like

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