(Help)Regular Expressions: Restrict Possible Usernames

So I specified the regex has to be alphanumeric with \w, then it can only have numbers at the end with zero or more of them with \d*. The * being zero or more times and being at the end. I should have fixed the capitalization problem with the i flag and repeating words with the g flag. These are two things I am not understanding which may or may not be the issue:

-Username cannot start with the number.
-Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.

**Also I have realized that I’m passing half the challenge with this code, the half being what I shouldn’t match, and the other half which isn’t working is the stuff I should be matching.

Thanks!


let username = "JackOfAllTrades";
let userCheck = /\w\d*$/ig; // 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/84.0.4147.89 Safari/537.36.

Challenge: Restrict Possible Usernames

Link to the challenge:

you have used the $ end of string character, but you have not said anything about start of string, you need to specify it

here you will need something a bit more complex

  • you need to use some characters to specify that “at least two characters long”
  • and something else to say “if it’s two characters long it can only have letters”

I would try doing the two separately, then try to merge the two

last but not least import, do not use the g flag with the test method

So I would’t have to use [a-z] because I’m already using /w which includes 0-9 so it can match that Z97 test case? Because Z97 is over two characters long but it doesn’t have only two letters in the beginning. So Z9 would have to be something like ZZ to work as only two characters? To set it to a minimum of two characters I read about curly braces technique but I dont think its required because its later on. The ^ at the beginning only makes the tests fail more but I think its because the rest of my regex is wrong.

It’s not passing the:
Your regex should match Z97 case.

Your code so far


let username = "JackOfAllTrades";
let userCheck = /^[a-z]\D+\d*$/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/84.0.4147.89 Safari/537.36.

Challenge: Restrict Possible Usernames

Link to the challenge:

1 Like

you have here “one letter, one non number, zero or more numbers”

you need to make it a bit more complex than this

remember that if it’s two character long it must be only letters, but if it’s three or more character long it’s fine also if there is only one letter and the rest are numbers

1 Like

So this Z97 case won’t pass because I have specified that the username can only start with 2 letters and Z97 has one at the beginning and the rest numbers. So I tried making an or | operator for one character and the rest numbers but the tests fail even more probably because the numbers aren’t staying at the end anymore. Its been a few days I have been trying to pass this a few hours at a time and now it’s really making me reconsider…

Your code so far


let username = "JackOfAllTrades";
let userCheck = /^[a-z]{2,}\d*$/i;
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/84.0.4147.89 Safari/537.36.

Challenge: Restrict Possible Usernames

Link to the challenge:

I passed it with this:

let username = "JackOfAllTrades";
let userCheck = /^[a-z97]{2,}\d*$/i;
let result = userCheck.test(username);

I shouldn’t have passed with this because this is for one test case but there is a way to do this challenge without the or operator right?

Its been a few days I have been trying to pass this a few hours at a time and now it’s really making me reconsider…

Regex will always be the bane of our existence, so don’t worry about that, it’s frustrating to me even after working for years.

You are correct that you should never have to special case a particular test case in your solution.

I got it to pass with:

(^[a-z]{2,}\d*$)|(^[a-z]+\d+)$

Explanation: I think it’s fine to use the OR operator here. We can basically split this up into two cases:

  • Cases where the username starts with at least two letters and 0+ digits
  • Cases where the username starts with one letter and at least one digit

When working with Regex, I like to use regexpal. Provides a cheat sheet for rules and I get instant feedback there instead of having to re-run tests. It also highlights the rules so I know if I’m doing something terribly wrong.

1 Like

Here is mine now:

let userCheck = /^[a-z]{2,}\d*$|^[a-z]\d{2,}$/i 

I didn’t want to look at your code so the answer isn’t given away, but I looked your explanation which I don’t think gave me much from what I already understood . I made two revelations. I forgot one thing which I never knew I had to do, which was use ^$ on both sides of the | operator. I was only applying these tags to one side (the first side) of the operator assuming that it would automatically apply to the other side… I fixed that and my code got me to “A1 should not match”, so I figured out if I use a quantifier {2,} at the end of my \d class, it would make it so the regex was looking for more than 2 digits at the end, so A1 can’t pass.

I was one hell of an experience though lol!