Https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/restrict-possible-usernames


let username = "JackOfAllTrades";
let userCheck = /\D/ig; // Change this line
let result = userCheck.test(username);


I do not know how to finish this.

So far your pattern is “containing one character that is not a digit”. What else do you need to add to your pattern?

I need to add the last two characters must be letters

How may I set that users must not using 2 letter usarnames.

If you’re looking for a list of tokens, regex101 has a good one as well as a interactive area to test your regular expression.

1 Like

How may I do the next setence? Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.

What have you tried?

I have made nothing bacause I have read the firsts problems about this.


/ 1[a-z]*9\w+[a-z]$/ig;

The flags are to use whatever letter in capital case or the another one.

w+ is for accepting from a to z and from 0 to 9.

[a-z]$ if for accepting as end part of it two letters.

1[a-z]*9 is for acceptiing numbers at the end.

try to put your regex here and see if the breakdown is what you expect:
(be sure to check JavaScript in the sidebar)

1 Like
let username = "JackOfAllTrades";
let userCheck = /[A-Z\s+]{2,}/ig; // Change this line
let result = userCheck.test(username);


console.log(username.match(userCheck));


How may I set the numbers at the end???

numbers can be represented with \d or with [0-9]

you have anchors to impose the end of the string

also do not use g with the test method, it has a different behaviour than what you would expect

It marked null

let username = "JackOfAllTrades";
let userCheck = /[A-Z\s+]{2,}[$0-9]/i; // Change this line

let result = userCheck.test(username);


console.log(username.match(userCheck));


Why do you have \s+ in there?
[$0-9] the $ token is the end-of-string token, so it shouldn’t be before anything in your expression. It also shouldn’t be inside the brackets, since it isn’t part of the range of digits.

I really really encourage you to use a pattern checking tool like regex101 to break down and test your pattern. Here is your most recent regular expression.

It marked null again

let username = "JackOfAllTrades";
let userCheck = /[A-Z]{2,}[0-9]$/i; // Change this line

let result = userCheck.test(username);


console.log(username.match(userCheck));


That pattern is not going to match “JackOfAllTrades” because “JackOfAllTrades” does not have a number at the end. Look at what your pattern requires and find where it does not match the challenge description. It feels like you’re trying to get other people to write your regular expression for you without putting much effort in on your own.

Ok. But that is my question: why is the reason why it does not work? I am not trying another person does my code. I am trying to understand it. I do no want to match Jack, I want to match Z97. I think [0-9]$ should works but it does not do it. The program in marking it as null when is should not do that.

[A-Z]{2,} What does this part of the pattern mean?

this means that it must end with a single number - is this what you want?

use the breakdown of the regexp if you are not sure what your expression does, it is really useful, I swear

1 Like