A Regex problem!

Tell us what’s happening:
everything is fine but the last task say that have to be at least two characters and if it just two characters it should be an alphabet.
anyone help me in this part ?

Your code so far


let username = "J";
let userCheck = /^[a-zA-z][a-zA-Z]\d|\D$/g; // Change this line
let result = userCheck.test(username);
console.log(result)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0.

Challenge: Restrict Possible Usernames

Link to the challenge:

This will match string starting with ab1(/^[a-z][a-z]\d/i)
or ending with a non-number(/\D/)


You can match only two alphabets with /^[a-z][a-z]$/i
Take a look at this thread
Try and practice various variants on regex101

2 Likes

Hey @Abdullatif_khayat!

Also make sure not to change the other lines of the code. The variable username needs to be this.

let username = "JackOfAllTrades";
1 Like

Actually, modifying that line doesn’t matter. That username setup is mainly for our testing purposes and we can change the value freely to test different cases. If you do, it wouldn’t mess up when the challenge is being graded since it will supplies its own test cases.

Hop this helps!

Hey @adina-t!

For this problem, that’s true but a lot of people post to the forum asking why their code doesn’t work and they accidentally changed other lines of code they weren’t supposed to.

In my opinion, it’s just good practice to only change the lines that are supposed to be changed. It would suck to bang your head against the wall wondering why the test wouldn’t pass all because you altered a line that you didn’t have to. :smile:

But thanks for pointing that out.

Happy coding!

1 Like

it should look like

let userCheck = /^[a-zA-Z]+([a-zA-Z]|\d\d$)+\d*$/;
1 Like