Restrict Possible Usernames - Question About Solution

Tell us what’s happening:
Hey everyone,

I have a question about the “Restrict Possible Usernames” regex task. I wonder why the 2 is in curly braces followed by a comma and a space. I understand the other parts of the solution. But I was stuck on the “2 character minimum” requirement for the longest time. Help!

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 11895.118.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.159 Safari/537.36.

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

The counter like that is a thing you will learn later in the curriculum, the first number in the curtly brackets is the minimum number of occurrences, the second is the max (if there is not a max, then any number that is at least the minimum is fine)

It can totally be solved with what you already know

/F{1, 2}/  (F occurs between 1 and 2 times)
/F{0, 1}/  (F occurs between 0 and 1 times) ( same as ? )
/F{0, }/   (F occurs between 0 and infinite times) (same as *)
/F{1, }/   (F occurs between 1 and infinite times) (same as +)

NOTE: between in this instance means including those numbers.

Problem solved.

@thegreencode I’m not sure where the code in your post is from, but it won’t pass the challenge. Here’s why:

let userCheck = /^[a-z] {2, }\d*$/i; // Change this line
                       ^   ^

Regexes are highly whitespace-sensitive, and neither of those spaces should be there. In this case, the regex will match the literal string "a {2, }", but it certainly won’t match "JackOfAllTrades".

The syntax for matching 𝑛 or more sequential occurrences is {𝑛,}, with no space inside the curly braces.

For the same reason:

Each of these will only work as expected after the space is removed.

Nope.

‘Dogggy’.match(/g{1,3}/); // ‘gg’

2 is between 1 AND 3.

Lionel is correct., the syntax I wrote is actually space sensitive., and incorrect.

Ok but what does:

‘Doggy’.match(/g{1,3}/); give you??

It matches between {a,b}

Between a AND b. Including a and b. As I wrote above.

I don’t think it’s even possible to have between a OR b., unless a and b were ranges.

Wouldn’t a or b imply it has to be one or the other?

2 is not 1 or 3 but yet it returned a match.

@camperextraordinaire Unless I’m missing something, it looks to me like @kerafyrm02’s original wording is correct (other than the whitespace issue): “between 1 and 2 times”. Perhaps “from 1 to 2” would be clearer, to show that the range is inclusive.

I think the issue is that the syntax highlighter is parsing the comment as code, so it looks like the “and” is the emphasized part.

@ilenia, @kerafyrm02, @camperextraordinaire, @lionel-rowe:

Thank you for all of your help. I guess the conversation got lively in here. That’s ok. That’s how we learn. I really appreciate all of you taking the time to help me out.

1 Like