RegEx Restrict Possible Usernames

RegEx Restrict Possible Usernames
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.


#10

Did I take a short cut?

/\D/g - my code passed.


#11

for that case you try this one: /\D{2,}\d+$/i;


#12

That definitely shouldn’t work… you can just put in a single letter and it will pass which breaks rule number 2. The unit tests for this question are pretty bad apparently.


#13

I’ve been working on this exercise and believed I found a solution but still somewhat confused.

My solution:

let username = "Jack33OfAllTrades"; 
let userCheck = /^\D{2,}\d*$/i // Change this line
let result = userCheck.test(username);

I believe the regex above can meet the rules as specified. My understanding is ^/D{2,} can also be replaced by ^[a-z][a-z]+

To test my understanding I broke down each part of the regex.

/^\D{2,}/

Above should match only if the first two characters are non digits, therefore taking care of the rule: “3) Usernames have to be at least two characters long. A two-letter username can only use alphabet letter characters.”

/^\D{2,}/i

This flag above is used to ignore care, therefore taking care of the rule: " 2) Username letters can be lowercase and uppercase."

/ \d*$/i

The above matches only if there are zero or more digits at the end of a string. Therefore taking care of the rule " 1) The only numbers in the username have to be at the end. There can be zero or more of them at the end."

The regex /^/D{2,}/i matches my understanding however the regex / \d*$/i did not return what I would expect. I was able to return true when I tested a username with numbers in the middle of a username.

Example Code:

let username = "Jack33OfAllTrades"; 
let userCheck = /\d*$/i // Change this line
let result = userCheck.test(username);

The result was true. I expected it to be false. When I use the whole regex /^\D{2,}\d*$/i I can meet the rules.


#15

Solution:
let userCheck = /[A-Za-z]/g;


#16

Used /[a-z]/ig and it worked.
Still trying to figure it out though. I used it to match characters at start.


#19

My solution:
let userCheck = /…/g;

It lets me pass the lesson, but I feel it shouldn’t work though. The .test() seems to ignore two of the requirements:
#1 It allows numbers at the beginning as long as the username is longer than 1 character.

#3 It allows a two-character username using only numbers.


#20

let username = “JackOfAllTrades”;

let userCheck = /\D/ig; // Change this line

let result = userCheck.test(username);


#21

I note that the {} notation to specify at least 2 characters is introduced in the next lession

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/specify-upper-and-lower-number-of-matches

whereas it is required and should go before