Restrict Possible Usernames Challenge

Can anybody tell me what’s wrong with my Regex? It passes all tests but one: “Your regex should not match 007”

let username = "JackOfAllTrades";
let userCheck = /[\w]{2}\d*$/i; 
let result = userCheck.test(username);
1 Like

[\w]{2} means exactly 2 of any word character (ie, a-z, A-Z, 0-9)

\d*$ means 0 or more of any digit characters at the end of the string

So putting those together, your RegEx will match any strings, as long as they have exactly 2 word characters at the beginning, and 0 or more digits at the end.

Here are a few pieces that might help you build your RegEx:
[a-z] means any character from a to z
\D means any non-digit character
{2,} means 2 or more
^ means at the start of the string

2 Likes

\w includes letter and digits as well. so for [\w]{2} the 00 matches. also \d means digit for 7, so 007 matches.

Please share the case-study link also so we can help better.

Thanks a lot for your help! It worked! Cheers! <3

1 Like

Thanks for your help!!!

i passed with the answer /\D./

\D stands for non-digit character and I used “.” as there are no restriction for the ending.

let username = “JackOfAllTrades”;
let usercheck = /[a-z]{2}\d*$/i
Useename atleast 2 characters only long not characters and number

I used /^[\D](\D+\d*|\d\d+)$/

  1. ^[\D] => non digital character at the start of the string
  2. \D+\d* => one or more non digital characters and then 0 or more digital characters
  3. | => or
  4. \d\d+ => a digital character and then another digital character or more
  5. $ => ending of string

Therefore 1 at the start and (2 OR 3) at the ending

This is my understanding of the problem. It’s very tricky.

The summary of the requirements are:

  1. At least 2 characters. If two-character username, both should be letters.
  2. Zero or more occurrence of number but only at the end.

Tricky requirements:

  1. Three-character usernames.
  2. Usernames with numbers not in the end.

With these in mind, we can conclude that a letter should be the first character:
/\D (length covered = 1)

If the username has no number in it (at least two character up to unknown length), we could use:
/\D+\D (length covered = 1 + n + 1)
I needed the 2nd \D to close the length and to not leave the regex open-ended (and possibly 1 length only if n is 0) with “+”.

If the username has a number, it should be at the end only.
/\D+\d (length = 1 + n + 1)
This will pass any username with a bunch of letters and least a number at the end.
But it will not pass the two-character requirement, e.g. “A1”.

Tip: Modify the \d a little further that will pass “A11” but not “A1”. Don’t forget to use the OR operator as well.