2nd lookahead dependant on 1st

only seems to check for 2nd lookahead (\d\d+) WITHIN the 1st 6 characters. (something to do with the first lookahead?)

if i put 2 \d’s in from 7th(ish) character, no match! but the first 6 and it’s a match!
also, if i remove the first lookahead, the \d\d+ works throughout.

any clue as to why much appreciated.

Your code so far


let sampleWord = "3a3n6n88n83";
let pwRegex = /(?=\w{6,})(?=\d\d+)/gi; // Change this line
let result = pwRegex.test(sampleWord);
console.log(result)


FALSE*

Your browser information:

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

Challenge: Positive and Negative Lookahead

Link to the challenge:

(i know it works if i add \w* before \d\d+ …i just not sure why)

here you are saying that the first two matched characters from the first lookahead must be numbers

am i?
you mean the leading 2 character from the first lookahead?
because i dont think that’s how it’s working.
(if there are 2 consecutive digits in the trailing 2 (or any) of the first lookahead, it gives ‘true’. (but not in succeding characters of original string))

the first lookahed says 6 or more characters
the second one says \d\d+, as the two lookaheads start from same position, the 6 characters matched by first lookahead must be digits

Try, it will fail for tttt77 but pass for 77tttt

i didn’t realise tttt77 failed, but my problem is, for example,…
why does abcdef66 fail?

aren’t the lookaheads both individually searching the whole string from the 1st character, for any match within the string?

they search individually, but the position from which they search is the same, a lookahead doesn’t advance the position of the regex

because there is not a sequence of 6+ characters that start with two numbers

1 Like

oh,
the lookahead is like a whole regex on it’s own…so you have to deal with the start of the string and work forwards kindaThing.
think i got it.
thanks

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.