Usernames: restrict possible usernames

What is the difference between

p+ - At least one p
p? - One or more p's
p{1,} - Sequence of at least 1 p

My code for the solution is /^[a-zA-Z+\d+$]|^[a-zA-Z{2}$]/
which I think means start with one or more letters and end with 1 or more numbers OR have exactly 2 letters.  Am I right in my understanding?

The errors I get are:

Your regex should not match J
Your regex should not match 007
Your regex should not match 9

Got it now

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

What is the difference between wholeStr.match(subString) and subString.test(wholeString)

/^[a-zA-Z]{1}\d+$ | ^[a-zA-Z]{2,}\d*$/

Why would this not work then. I added the first expression
^[a-zA-Z]{1}\d+$ to specify 1 letter ending with at least 1 number (matching J324)
and then the | which should be OR
and then the original expression
/^[a-zA-z]{2,}\d*$ start with 2 or more letters, end with 0 or more digits (matches dK and doik324)

So why is it saying does not match Oceans11 or Jack or RegexGuru

Man, this regex thing is frustrating. Just the fact that I put spaces before and after the | made it not work.

The expression I gave above works but only when I take out those spaces

The problem with this challenge is that there are a couple ways of doing it that pass all the given tests, but that don’t adhere to the username rules set out in the explanation. I’ve bolded numbers one and three because this is where the inconsistency arises.

Here are some simple rules that users have to follow when creating their username.

1) The only numbers in the username have to be at the end. There can be zero or more of them at the end.

  1. Username letters can be lowercase and uppercase.

3) Usernames have to be at least two characters long. A two-letter username can only use alphabet letter characters.

Change the regex userCheck to fit the constraints listed above.

The challenge lays out these constraints but then doesn’t actually test for them, allowing you to pass it without fulfilling these requirements. My first passing attempt (I don’t remember what I used now) did exactly that. It passed all the tests, so I was allowed to move on, but it didn’t fulfill constraint one or three.

On another note, the solution given in the hints section: /[1]{2,}\d*$/i;, while passing all the tests, doesn’t allow for a username such as “D48”, for example, because it requires the first two characters to be letters. But the constraints only specify that the characters must be letters only when the username is only 2 characters long. A 3 character username can technically only contain one letter and the rest numbers.

As for your solution @shashgo: /[2]{1}\d+$ | [3]{2,}\d*$/. While it does pass the tests and allow you to move on, it likewise doesn’t adhere to the third constraint. A username such as “H8” is returned as true when it should be false.

I think I have found a solution that passes all the test and still follows all the constraints set out in the challenge that aren’t tested for: /[4]{1}\d{2,}$|[5]{2,}\d*$/i;.

Sorry for the long-windedness, but I was confused by the given constraints in this test and the possible solutions, so hopefully the next person to search the forum about this issue can find what they’re looking for in this post.


  1. a-z ↩︎

  2. a-zA-Z ↩︎

  3. a-zA-Z ↩︎

  4. a-z ↩︎

  5. a-z ↩︎

3 Likes

your regex may also pass a username like a333b, can you check?

Yes, you’re absolutely right, damn! I thought I had it all figured out. I tried testing with ‘joe56namath’ and it was false, so I assumed it was all good.

EDIT: Ok it was an easy fix though, just forgot a dollar sign. I corrected it in the post. (At least I think it’s all fixed, let me know if there are other outliers.)

Just wanted to say: Massive thank you! for your contribution on this challenge.
I’ve been stuck in it until I found your well-written out comment.

The “at least 2 digits” chapter newer comes before. I didn’t get the solution without the hint just with the lectures before. Really bad.

1 Like

sure you can. You have + and * at your disposal
here, this match at least two as: /aa+/

I am missing something regarding the ‘g’ flag. I used the following regex to solve the challenge but, at first I had the g flag enabled and it failed. I can’t seem to understand why that is…thoughts?
^[a-z]{2,}\d* | ^[a-z]+\d{2,}/i;

the documentation talks about what happens with the g flag: