Restrict Possible Usernames -- help

Restrict Possible Usernames -- help
0.0 0

#1

Tell us what’s happening:

I don’t get how considering the regex lessons so far do I ensure the number will only be at the end and to limit the characters to be at least 2 characters long and they only use alphabets if 2 characters long

Your code so far


let username = "JackOfAllTrades";
let userCheck = /\w+\d/i; // Change this line
let result = userCheck.test(username);

Your browser information:

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

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


#2

@JM-Mendez @kevinSmith


#3

See https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/match-ending-string-patterns

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

See https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/match-letters-of-the-alphabet

Now you just need to think about how to use all of these and tie it all together to create a solution. You can use a site like regexr.com to play around with the various expressions to understand what gets matched.


#4

Thanks, two of the links you pasted here are future lessons why do this at this stage then?


#5

Because coding. lol. But seriously, think of it as another lesson. Many times in the dev world you’ll have to research things beyond what the specs ask, just so that you can accomplish a task.

So if you look at it as a lesson in researching, then it’s not too soon :slight_smile:


#6

Actually, there was only one lesson which was in the future. I have edited my reply to reflect what I meant to write the first time.


#7

I absolutely understand, this was something FCC encouraged a lot in the previous version but I think its absolutely stupid to not mention that you might have to search on google or read the docs or something. It is like thinking outside the box for the sake of thinking outside the box, it is pretentious.

Earlier they made you learn things and then didn’t have a lesson explaining that same thing and that is true now.


#8

You can also do something like this :

let userCheck = /[a-z][a-z]+[\d]*$/i;

I don’t think it’s using any of the following lessons.


#10

Thanks that is good, but what in the code ensures the password is at least 2 chars long and what part ensures if 2 then only alphabets? @randelldawson @JM-Mendez @Capucine


#11

The first [a-z] ensures that the first element is a letter,
the second [a-z] does the same for the second element
and the + lets the password be longer if needed.
Because there is two [a-z], the password will be at least 2 characters long.

The code would be better with an ^ at the beggining, because with my previous code something like 7password would pass, I think.

Hope this helps :slight_smile:


#12

Definitely helped, thanks!


#13

For the second [a-z] I accidentally did [A-Z] and that also worked. Any insights on that?


#14

Yes, /i makes it so the regex will look for both uppercase and lowercase letters, no matter which one you wrote in your expression!


#15

Yes! of course. Dumb thing to ask


#16

Don’t worry, there is no dumb question :slight_smile: