Regular Expressions - Restrict Possible Usernames

Tell us what’s happening:

Can’t find an answer to match the string z97 …

Your code so far

let username = "JackOfAllTrades";
let userCheck = /^[A-Za-z]+[A-Za-z][\d+|\d*]$/; // Change this line
let result = userCheck.test(username);
console.log(result);

Your browser information:

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

Challenge Information:

Regular Expressions - Restrict Possible Usernames

you need to build the two different options, there is an operator you can use in regex you met not many lessons ago

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/match-a-literal-string-with-different-possibilities

Welcome to the forum @TheCaptain10

Your regex is also failing:

Write out in a sentence what your regex is working out.

Happy coding

1. Usernames can only use alphanumeric characters.
2. The only numbers in the username have to be at the end. There can be zero or more of them at the end. Username cannot start with the number.
3. Username letters can be lowercase and uppercase.
4. Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.

You probably know that requirements 1 though 3 can be matched, easily with a single logic search. Something like

^[A-z]+[0-9]*$

However, step 4 requires alternative logic of search. (using the symbol | ) because if the length of the username is only two characters, these must be alphabet ones.
In other words. We have two alternatives:
Start with two alpha characters ^[A-z]{2, }
or start with an alpha character followed by at least two digits ^[A-z][0-9]{2, }

The format, then, all together might be similar to:

/^(alternative1|alternative2)[0-9]*$/

Yeah. After some time I realized that there needed to be two cases and the OR operator should be used to meet the requirements. Thanks for the reply.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.