Regex Possible Username Problem

Tell us what’s happening:
Hi,
these were the instructions for the regex problem:
You need to check all the usernames in a database. Here are some simple rules that users have to follow when creating their username.

  1. Usernames can only use alpha-numeric 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.

Kindly help me understand why this is wrong with my solution. (i have written the comments below the code)

Your code so far


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

/*
^[a-z][a-z]+ : first two characters should be at least two alphabet letters and optional number(s) at the end

| : or
^[a-z]\d\d+$ : first letter is an alphabet followed by at least 2 numeric characters whereby the last character is a digit
*/

Your browser information:

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

Challenge: Restrict Possible Usernames

Link to the challenge:

Hello,

It’s because of your /g flag, if you remove it the test should pass.
You can check the explanation of why this happens on this post:

The “Restrict Possible Usernames” lesson is bugged - JavaScript - The freeCodeCamp Forum

1 Like

Thank you.
From that post I’ve understood i should not use the global flag when the string has one ‘word’.