Regular expression help needed

Tell us what’s happening:
Describe your issue in detail here.
test fails because of**( Your regex should not match the string**
BadUs3rnam3)
how to fix this ??

Your code so far

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

Your browser information:

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

Challenge: Restrict Possible Usernames

Link to the challenge:

1 Like

you may break it down into two parts:
first one should check that the string has at least two letters {2,} and zero or more numbers.

aa
aaaaa123
aaa2324234

second part of the regexp should only check for the last case when string has 1 letter followed by 2 numbers.
a12
possible solution: /^([a-z]{2,}\d*|[a-z]\d{2})$/i

upd:
another one approach is to define basic rule like: the string should start from at least one letter and may be followed by zero or more numbers. And then add two additional checks:

  1. the string length should be at least 3 symbols
    or
  2. the string should consist of two letters

/^(?=.{3,}|[a-z]{2})[a-z]+\d*$/i

1 Like

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