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.
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.
@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.
@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.
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.