Positive and Negative Lookahead can you help clarify something with regex?

Positive and Negative Lookahead can you help clarify something with regex?
0

#1

Tell us what’s happening:
So the only way I could solve the challenge was by using only ONE lookahead, but the challenge didn’t want that. I gave up and found \D* which are none digits. I read some of the forum posts here and they are saying that the reason why there is \D is because the lookahead needs to check the previous characters before the digits and those will be non digits thus \D, so my question is actually this, if you have two (xxxx)(yyyy) do they act like an && to match? something like (xxxx)&&(yyyy) before they match? Because with the brackets [][][] they kinda operate per character unless you put + or *, so this part is really confusing me.

Your code so far


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

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead


#2

It’s alright to be confused by this, because it’s a hard one to explain, though I’ll give it a go anyway

With this in particular, and a lot of regex related questions, they can be solved by putting yourself “in the mind” of the regex engine

Imagine you are a regex engine, and you’ve been given a regex command and a string to check. You traverse the string, examining at each step the current character you’re in front of. You take another step each time to look at the next character.

Imagine now you’re told to look ahead. This means you examine the next few characters to look for a match without moving your position

You look ahead, and check that, yes/no (positive/negative), the next few characters satisfy the lookahead, and continue trying to match the rest of the regex in the usual way

When we have two lookaheads at the same time, we are looking from the same position. That’s why it almost seems like there’s a logical AND there, because we check the first lookahead, check the second lookahead, and then try match the rest of the regex

The reason that \D* was needed, is because when looking ahead to check there’s two digits, we don’t care about them being right in front of us, so we are told “0 or more non-digits right in front of us, with two digits”


#3

I think this is one of those things where I really need to deepdive to understand what’s going on. I just finished grouping in FCC and it seems lookahead just behaves differently. But I’ll go with that there is an implicit && with two lookaheads and I’ll try to understand it from there. I’ve been pacing for 5 minutes and really can’t get it right now xD still, thanks :smiley:


#4

it’s one of those topics where everything will just magically ‘click’ at some point and in hindsight everything will seem obvious