Positive and Negative Lookahead - not working?

Tell us what’s happening:
I have written a Regex which meets all of the expected outcomes except one.
The outcome it does not meet is: Your regex should not match "banan1".

What am I missing? Or more importantly, what did i forget to include?

Your code so far


let sampleWord = "astronaut";
let pwRegex = /(?=\w{5,})(?=\D*\d)/i; // Change this line
let result = pwRegex.test(sampleWord);

You must be careful that \w is not word only, but it also includes number characters.

I don’t think the (?=\w{5,}) is your problem here.

I’m relatively new to regex too, but I believe the left hand side (between parenthesis) means the entire string must be at least 5 characters long. So it looks like this satisfies the first requirement of the task.

The right hand side (between parenthesis) means the entire string must be made up of zero or more non-digits, followed by a single digit… therefore matching banan1. This part doesn’t meet the task requirements.

See where you might have gone wrong now?

1 Like