Usernames: restrict possible usernames

Usernames: restrict possible usernames
0

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

p+ - one or more p’s
p? - one p or no p
p(1,} - one or more p’s

There is no difference between the 1st and 3rd.

^[a-zA-Z+\d+$]

The above looks for the string to start with any letter (uppercase or lowercase) OR the + character OR a digit OR the + character or the $ character.

Any character you put between the [ and ] (a character set) is interpreted as the actual character unless it is a meta character (preceded by the backslash) like the \d you have above.

I will give you a small hint here. If you the string to start with a letter (upper case or lower case) and be followed by one or more digits, then you would write:

/^[a-zA-Z]\d+/

You will notice I only put the letters in the character set. The \d+ comes after the character set.

Got it now

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

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

The regex you show above would not allow a username such as J37 which fulfills the requirements for a valid username.

/^[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: /^[a-z]{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: /^[a-zA-Z]{1}\d+$ | ^[a-zA-Z]{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: /^[a-z]{1}\d{2,}$|^[a-z]{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.

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