Positive and negative Lookahead - what is wrong

Hello, I am trying to do this exercise and I am stuck, actually I do not understand why it does not accept one condition(this is the only one missing to pass this task) :
Your regex should match "bana12"
when this is
Passed
Your regex should match "abc123"

How come it can pass “abc123” and not “bana12”. What should I change in the code to have this fixed? I spent 40min on this and I cannot notice that.
Thank you in advance
My code looks like that:

Your code so far


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

Your browser information:

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

Challenge: Positive and Negative Lookahead

Link to the challenge:

both lookaheads start looking from same position.
so you have that the second lookahead wants one or zero non number character ([^0-9]), than one word character ([a-zA-Z0-9_]) and then at least two numbers

I don’t know why one pass and he other not, but your code doesn’t match requirements

EDIT: I know, one moment for explanation

this passed because the second lookahead find the substring bc123, which also is at least five characters long so correspond to also first lookahed

here the second lookahead find substring na12, which is only 4 characters and doesn’t match the first lookahead

Thank you for the answer, but I do not understand why the second lookahead does not find substring “ana12”, if it takes 5 characters from “abc123” why it cannot do it in this case aswell.

the second lookahead you have

  • \D? one or zero non digit
  • \w one word character
  • \d{2,} and then at least two digits

so it can match only one or two characters before the numbers
bana12 has the substring na12 that matches this.

1 Like

okay I understand now, thank you

here is the code where all conditions are met
let pwRegex = /^\D(?=\w{5,})(?=\D?\w+\d{2,})/