REGEX(restrict usernames)

REGEX(restrict usernames)
0

Tell us what’s happening:

Hi guys, I managed to pass all the tests except,
"Your regex should not match J "
and
"Your regex should not match A1 "
I tried multiple ways to create this code and this is the one that gave me the best results, on what I learned so far, but I guess not enough to pass.

Thank you in advance for your help.

Your code so far


let username = "JackOfAllTrades";
let userCheck = /^[A-Z]+\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/79.0.3945.117 Safari/537.36.

Challenge: Restrict Possible Usernames

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/restrict-possible-usernames

this is matching one or more letters, right?
and then zero or more numbers

total min length is 1

you need to add something more

try maybe playing with a regex online tester, one of many: https://regex101.com/

Doesn’t this [A-Z] match all the letters, lower case and upper case when we use the i flag?

Yes, it does. Your main issue is that you need your username to:

Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.

A hint:

You need to use the {} quantifier

Well then, the quantifier character {} is not yet in the curriculum, so I am actually trying to solve a challenge with something I didn’t even learn yet. Thanks for that freeCodeCamp.

you absolutely don’t need that quantifier, you can use those you already know
“at least two characters” === “one character followed by one or more characters”
you can write that second version with what was previosuly in the curriculum

there is also the version “one character followed by one character followed by zero or more characters”

Ah right. My bad. Ignore me, and listen to @ieahleen.

I built this from what I “kinda” understood that you said @ieahleen


/^[A-z][A-z]+\d*$/i;


but still didn’t manage to pass this test " Your regex should match Z97"…

that is an other requirement…it comes from this:

  1. Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.

as you need the two letters at the beginning for that thing, but if the username is longer it can have also une single alphabet character at the beginning

you need something more complex than this, maybe using an OR

Yeah, the challenge requires you to know a bit about the quantifier {} character or the OR | character…either one of them are not in the previous lessons…

Thanks for your time, I managed to get all of them right except two tests taking into consideration that it has a couple of important characters that can help you solve the task or at least help you understand the concept better but are not available in previous lessons, so that’s enough for me.

maybe you are right that the or is not in previous challenges, but you can have the same effect of the quantifiers with what you already know, it is just longer to write

I managed to find some videos from freeCodeCamp explaining a bit more the exercise, so I feel that I have a …ok understanding of the challenge now, using the {} and the OR | characters on it. Thanks again.

I just went through this challenge and I have to say, it was a little annoying/tricky. I ended up using the OR logic which was presented earlier in the Match a Literal String with Different Possibilities module.

If anyone knows for sure that there is a solution that does not require {} or |, I’d love to know about it.

1 Like

one way or an other I am pretty sure you need to use the OR
as “A007” is a valid username, but “A0” is not, and “AA” again is a valid username

1 Like

I solved this using grouping("()") and the OR character, but grouping had definitely not been presented earlier in the curriculum. I am still unclear as to how this would be solved without grouping.
For example

/^\D[\D|\d]\d*$/ will match both “Z97” and “A9”

Does anyone have a solution that doesn’t involve grouping and the OR character?