Please help me understand why this code is getting errors

Hi. OK, I’ve seen the solution but I still tried to make my own code.
What seems to be wrong with it is that it accepts numbers in the middle because those were the error messages in the challenge console. I really tried to reason it myself. Thank you.
Challenge: Regular Expressions: Restrict possible usernames.

let username = "JackOfAllTrades";
let userCheck = /^[a-z][a-z][d+$]|[a-z][a-z]/i; // Change this line
let result = userCheck.test(username);

Errors thrown:

// running tests
Your regex should not match BadUs3rnam3
Your regex should match Z97
Your regex should not match c57bT3
// tests completed

This is a source of errors - the [] creates a character list, so this translates to "Match a single character that is d or + or $.

1 Like

@nhcarrigan Thank you very much. I’ll change that.

I changed my code to this:

let userCheck = /^[a-z][a-z]\d+$|[a-z][a-z]|[a-z][0-9]+$/i;

Now it matches Z97 but not A1

I’m close. I understand the solution given better. So it was worthwhile to try this. Thank you.

1 Like