I think that I've found an error in Regular Expressions: Restrict Possible Usernames

Hello this is my first post here I’m going to try to do my best.

After some time trying to solve this exercise I decided to look the answer. The thing is that in the answer quantity specifiers {} are used to solve the exercise but the use of quantity specifiers is not teached until Specify Upper and Lower Number of Matches.
Has another way to solve that excercise without quantity specifiers ?

Thanks!

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

@Japer Yeah, there’s definitely a few ways to do this. I only used \D and \w to solve it. It doesn’t catch every possible username, but it passes all of the tests.

There are often many ways to solve a problem some of the solutions that are listed in the guide will include information that a camper may not have researched yet, but that isn’t the only way to do it.

But in your code the numbers don’t necessarily to be at the end. Although it passes the test due to another error in that ecxercise.

What do you mean? I am not sure I understand you, the assignment says that the numbers need to be at the end and in my regex they are at the end. However, now I am seeing that i missed a plus at the end of the second character class. This should be more general. Or You can use some of the character classes that @camper suggested. Anyway, there are multiple solutions to this problem and you don’t need anything more than what you have covered so far.

/[a-z][a-z]+\d*/i

Try your regex with “JackOfAllT9rades” for example. The .test() method it’s going to throw true but the correct answer is false because numbers need to be at the end and not in the middle. @RadoIIvanov

And the solution to that, using @RadoIIvanov’s suggestion might be

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

Limiting it to start with chars and optionally end with numbers. (Had to double-check that, make sure that start and end limiters have been covered by this point in the curriculum) :wink:

1 Like

Yeap, missed to use the anchors :slight_smile:. Yesterday was a weird day, sorry about that.