My regex code is right, but the challenge won't pass

Hey there, I am doing a regex challenge on the beta site. Here it is: https://beta.freecodecamp.com/en/challenges/regular-expressions/restrict-possible-usernames. I have taken my regex and put it into this site and all the parameters pass: https://regex101.com/r/fqeFDT/1. If I have made an error can someone please point it out? I am failing the 1st and 4th check.

And here is my code:

let username = "JackOfAllTrades";
let userCheck = /^[a-z]{2}([a-z]+)?(\d+)?$/gi; // Change this line
let result = userCheck.test(username);

I’m not sure why your regex is failing the tests, but it might have something to do with the g flag. I tried a couple of different regexs that met the requirements without using the g flag, and they both passed the tests. (Basically use {1,} instead of combining + and g to match one or more)

Remove the g flag - the g flag is for incremental matching - it is not needed for this exercise - it causes test cases to fail because the same regex variable is used for every test case

This regex is a simpler equivalent of yours: /^[a-z]{2,}\d*$/i

However the exercise has issues - username “007” should be allowed since it has more than 2 characters and has numbers only at the end - either the test case should be corrected or the exercise statement should be updated to restrict usernames to start with a letter or possibly 2 letters

2 Likes

Thanks for the help. Could you explain what {2,} means?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#quantifiers

The general brace expression is {m,n} that matches between m and n occurrences of the preceding item - no n means match at least m occurrences - no m means match at most n occurrences - {2,} means match at least 2 occurrences - {,2} means match at most 2 occurrences - you know what {2} means

1 Like

This let me past the test too,

let userCheck = /[a-z][a-z]/gi;

but it feels incorrect… Why didn’t I had to declare \d?

I think I get it,

“kk66d” should not pass, but it does with my code and in the test there there are only strings given with numbers at the beginning or end.

My code is right but it won’t let me pass the test, here is my code:

/[a-z][a-z]\w*\w\w/gi

result:Your regex should match JACK
Your regex should match RegexGuru

My problem resolved after I changed my regex to

/[a-z][a-z]\w*\w\w/i

Hi Alimama,
even though your code passes the test, it’s not quite correct. Here are few usernames that shouldn’t go through, but your RegEx doesn’t restrict them:

  • 1Jack (Since the only digits in the username have to be at the end)
  • Ja33ck (Digits should be at the end)
  • Ja (should pass)
    etc…

Your RegEx allows digits in the begging and in the middle, and makes the word at least 4 characters long.

The easiest solution to the challenge should be:

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

where:

^[a-z] guarantees that your username starts with letter;
^[a-z]{2,} guarantees that it starts with letters and is at least 2 characters long;
\d* says it can have 0 or more digits.
\d*$ guarantees that digits are only at the end.
i - ignore case.

Hope this helps,
Good luck.

8 Likes

That is an excellent breakdown to explain the solution! Thank you!

why is there no g flag?

Can’t tell exactly, but almost every time I’m using ‘g’ flag some of the tests fail (even tho I know that RegEx is correct).

The most efficient regex I could come up with was /^[a-z].\d*?/i