Regular Expressions - Restrict Possible Usernames

Some feedback about my solution please:

I have managed to fulfill the conditions so that my Regex works but I am not too confident I did it right.

  1. The username has to be at least two letters long but if it is two letters long it should not have numbers: ^[a-z]{2}$
  2. The username should not start with numbers but may end with numbers or not: ^\D[a-z]+\d*$

But then it hit me: “A12” or “B523” should also be valid names so I added this to the pattern: ^[a-z]\d\d\d*$

Is this OK?

let username = "JackOfAllTrades";
let userCheck = /^[a-z]{2}$|^\D[a-z]+\d*$|^[a-z]\d\d\d*$/i; // Change this line
let result = userCheck.test(username);

console.log(userCheck.test("JACK"));
console.log(userCheck.test("J"));
console.log(userCheck.test("Jo"));
console.log(userCheck.test("Oceans11"));
console.log(userCheck.test("RegexGuru"));
console.log(userCheck.test("007"));
console.log(userCheck.test("9"));
console.log(userCheck.test("A1"));
console.log(userCheck.test("BadUs3rnam3"));
console.log(userCheck.test("Z97"));
console.log(userCheck.test("c57bT3"));
console.log(userCheck.test("AB1"));
console.log(userCheck.test("J%4"));

Your browser information:

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

Challenge: Regular Expressions - Restrict Possible Usernames

Link to the challenge:

It passes the test so I’d say it’s OK. Could it be simplified a little? Yes.

Yet, I feel that my solution is clumsy… Still, I understand it… I will check other solutions to learn about this subject.