Positive and Negative Lookahead in Regex

I try all possible ways but I didn’t pass this challenge, please Help.
this my code

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

this is fail test

pass freecodecamp

well, the test method will tell if there is part of the string that correspond to the pattern, pass99 correspond to the pattern, so it returns true.
Maybe you want to specify where the match should start?

Can you explain more please??

if the test method search for a substring that matches the pattern, you need to anchor the pattern to the beginning and/or end of the string, to be sure that it is not just a random portion in the middle that pass, but the whole string

pass99 is a substring that matches the pattern, but if you anchor the pattern to the beginning of the string, the substring that matches must start at the beginning.
There are these nice things called anchors… they are uber useful.

After looking at the solution, I found that the second part needs \w*. Why?
I tried looking all over the internet for why a two-part regex would need \w*\d{2} to satisfy the requirement that there are at least two consecutive digits and that there are at least 5 characters and that it starts with a non-digit.

what does the \w* do? in this instance? it seems like /^D(?=\w{5})(?=d{2})/ satisfies all that. I passed the test, but I just don’t understand why it passes with the \w* added to the beginning of the second part of the regex.

the two lookaheads start looking from the same position, and remember that the two digits can be anywhere in the string, so you need to account that

I was really stuck here and I still don’t understand why it’s necessary to use

  1. (?=\w+\d{2}) instead of
  2. (?=\D+\d{2}).

I am checking with ^\D at the beginning, then the length (works fine) but I do not understand why 1) works with all the strings and 2) does not match the “astr1on11aut” case.

Thanks for your help!

if you use \D+\d{2} it means that you are not allowing numbers before the two consecutive ones

ah, ok, so this means \D+\d{2} does generally not allow any single numbers before the two consecutive ones?

If so, thank you very much, that helped a lot!

not generally
here you are matching from start of the string