Regular Expressions: Restrict Possible Usernames

Regular Expressions: Restrict Possible Usernames
0.0 0

#1

Hi sorry for creating another of this similar topics…
my code is able to pass the test for checking numbers
But for letters in beginning it fails. Can someone please explain to me why what im doing fails to pass for letters in combination with checking for numbers.

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

#2

using the \d$ part to check for numbers at end works only if its alone.


#3

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

This condition doesn’t hold because yours only check for exactly one digit instead of zero or more.

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

This condition doesn’t hold. How does yours check that two letter username must contain only alphabets? For example, a1 passes; but, it shouldn’t. (However, this won’t affect passing the tests, which you can read about from below link)

This challenge has some problems to get it perfectly.


#4

Let’s take a look at the requirements again:

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

You haven’t successfully added the right modifier for zero or more matches, your current code finds one digit

  1. Username letters can be lowercase and uppercase.

You’ve correctly added the case-insensitive flag, nice!

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

You haven’t correctly added the right modifier for two or more letters. Currently your code finds one or more letters

You’re very close to the right answer really, just take a second to look over how to match the correct numbers


#5

Hmm,
number 3. seems to be tricky in figuring out.
i got till here /^[a-z]+\d+$/ig

i cant figure out the right way to search for two or more characters. Using * instead of + gives same errors too


#6

tried this /^[a-zA-Z]*\d+$/ig;


#7

can u explain the use of {2,} is it similar to [a-z]{1,} == [a-z]+


#8

Yep, those two are exactly the same thing. For {2,}, this asserts at least two.


#9

This still fails for me /^[a-z]{2,}[\d]*[a-z][\d]{1,}$/i;

and also if i remove [\d] from the beginning checker: /^[a-z]{2,}*[a-z][\d]{1,}$/i;

it fails all tests… why does that happen?


#10

If you are referring to my solution, then you are missing |, which is for alternative pattern.


#11

ok so i try this for my solution , trying to incorporate things from ur solution
reaching here /^[a-z]{2,}[\d]{1,}$/i
but it still fails.

am i still doing something wrong?


#12

What you wrote requires at least one digit at the end.

The first part of the solution, which solves most cases is this
/^[a-z]{2,}[\d]*$/i

This takes at least two alphabets and optional numbers.

This can pass

  • Two letter username has only alphabets
  • Username ends with optional numbers, if username.length is at least 3

But this will fail the case where username is at least 3 characters long and begins with an alphabet followed by numbers. This is where the second part of my solution kicks in. However, this won’t affect passing the tests.


#13

can i do something like {0,} ?


#14

It is possible, but for {0,} and {1,} you always have * and +.