RegEx Restrict Possible Usernames

RegEx Restrict Possible Usernames
0.0 0

#1

Tell us what’s happening:

how to make usernames like ja4ck, du76de, or son495ja returns false/invalid?
one of the rules is number characters must be at the end of usernames, like jack4, dude76 or sonja495 are valid.

Your code so far


let username = "JackOfAllTrades";
let userCheck = /\D+\D*?\w+$/gi; // Change this line
let result = userCheck.test(username);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/restrict-possible-usernames/


#2

You got it so hard dear, it’s not so hard to match what it needs.

For regex, you may go step-by-step, unless you will make something very mess!

First, let look at your regex and find out the errors

/\D+\D*?\w+$/gi

It means starts with no any digit(\D) and + means 1 or more. So it would match A for example.

\D*? means no digit 0 or any! This part is a little nonsense, because you could apply the filter with previous part.

\w+$ means any char(one or more) (including chars and digits) at the end ($) of the string. this part could match 90A(which is wrong), as same as a90.

Now let’s make it from start, as assignment needs you:
It should not start with digits, digits could be place at the end tho. so use \D, or [A-Z]
Minimum 2 length so {2,0}
Case-insensitive /i

Possible working regex:
/[A-Z]{2,}\d*/i
or
/\D{2,0}\d*/

Please note \D is not [A-Z], but both would pass the assignment

Keep going on great work, happy programming


#3

Ah… yes! I don’t even remember why I put the \D*? inside…
thank you for your great explanation! It all makes sense now
I’ts a nice feeling to get learning from mistakes
Bless your day!


#4

A more simple approach is:

/^[a-z][a-z]+\d*$/i


#5

My answer:
/[a-z]|d$/ig


#6

My answer

/[a-z]\d?/gi


#7

my answer is:/ \D\w/
It passed.


#8

@henrymm

And
@prograk (remove g flag also)

It may passes with strings in the test, but it’s wrong, please considering #3 in challenge

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

Your / \D\w/, /[a-z]\d?/i and could match C4 for instance which is wrong, or matches AB99A(wrong too). Because both \D and [a-z] matches one non-digit char. As rule #3 both chars should not be digit if the string is 2 size length.

@Pifko6
Did you mean /[a-z]\d$/ig instead of /[a-z]|d$/ig ?(\ instead of |) either ways both wrong.(also remove g flag)

Thank you so much for sharing your patterns, I just opened an issue in FCC repository, and hope soon it could be fixed.


#9

Thank you so much for your guidance.