(Regex) Code is equivalent to solution and isn't working

As far as I can tell, my regex code is the equivalent to the solution code (e.g. swaps [a-z] for \D) but it’s failing several tests.

I have no idea why this is happening.

I’ve commented out the solution I based my answer from. Can anyone help me out please?

Your code so far


let username = "c57bT3";
let userCheck = /^\D(\d\d)|(\D+\d*)$/; // Change this line
let result = userCheck.test(username);
let matcher = username.match(userCheck)

console.log(result)
console.log(matcher)   


/* ^ - start of input
 [a-z] - first character is a letter
 [0-9][0-9]+ - ends with two or more numbers
 | - or
 [a-z]+ - has one or more letters next
 \d* - and ends with zero or more numbers
 $ - end of input
 i - ignore case of input*/

Other solutions I have tried are:


let userCheck = /^\D(\D|\d\D)/ 

let userCheck = /^\D(?=\D{1})(?=\D+\w*$)/; // Change this line

let userCheck = /^\D(\D|\d\w+)/; // Change this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0.

Challenge: Restrict Possible Usernames

Link to the challenge:

Couple remarks:

  1. \D means anything excluding digits (including this :poop: emoji). You might want to be a bit more specific here
  2. Pattern \D+\d* allows for username with only one non-digit character, like 'i' which breaks rule #4. You need to fix that too
1 Like

Thanks for the tips there.

I reformatted my code from scratch and the solution that I got to was:

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


/*
/^[a-zA-Z] - must start with a letter
([a-zA-Z]$ - if username is two digits, second character must be a letter
| or
\d{2,}$ - if username is a letter followed by a combination of digits, there cannot be a letter at the end and username is at least three characters long
| or 
[a-zA-Z]{2,}\d*)$/; - username is at least three characters long, any combination of letters is allowed and the username may or may not end in a combination of digits
*/

Being more specific definitely helped.