Positive and Negative Lookahead (More Explanations)

The double lookaheads used in this challenge was not explained. One lookahead at a time were used in the examples for explanation, but the challenge was something else.
So I spent an uncertain amount of time trying to figure out how multiple lookaheads work.
So, this was what I found out.

Using One Lookahead

If you’re reading this, then you probably understand how to use a single lookahead.
For example:

const str = "Is this all? Find more";
const pattern _pos = /is(?= all)/;      //*+ve lookahead, notice the space btw '=' and 'all'.*
const pattern_neg = /is(?! all)/i;      //*Negative lookahead, notice the ignore flag*.

console.log( pattern_pos.test(str) );        //*output 1*
console.log( str.match(pattern_pos) )       //*output 2*
console.log( pattern_neg.test(str) );       //*output 3*
console.log( str.match(pattern_neg) )       //*output 4*

output 1 will give true
output 2 will give is
output 3 will give true
output 4 will give Is

In the regex pattern_pos above, If the space between the ‘=’ and ‘all’ is removed, output 1 and 2 will give false and null respectively.
I’ll read the pattern_pos as: "Look for an ‘is’ that is followed by a space and an ‘all’ ".
This matches the ‘is’ in ‘this all’.

In the regex pattern_neg above, I’ll read the pattern as: “find me any other ‘is’ that isn’t being followed by an ’ all’ .”
Output 3 resulted to true because there is actually an ‘is’ that isn’t being followed by an ’ all’ in the string.
Output 4 resulted to ‘Is’ (note it has a capital ‘I’ ), because the ‘Is’ at the beginning of the string isn’t being followed by an ‘all’.

Using 2 or more lookaheads

Let’s adjust the code above to use 2 lookaheads at the same time.

const str = "Is this all? Find more";
const pattern  = /(?= all)(?! this)/ig;     //*Notice the space btw '=' and 'all',* 
                                            //*also the space between '!' and 'this'.* 

console.log( pattern.test(str) )            //output 1
console.log( str.match(pattern) )           //output 2
console.log( str.match(pattern).length )    //output 3

output 1 gives true
output 2 gives null
output 3 gives 1

In the regex pattern above, I’ll read the pattern as: “find me something that is being followed by a ’space’ and an ’all’, AND is not being followed by a ’space’ and a ’this’ .”

Output 1 resulted to true because there is actually something that passed this test.
Looking at the string, you’ll find out that ’this’ is being followed by a ’space’ and an ’all’ and not followed by a ’this’.

Output 2 resulted to null, because that something was not specified.

Output 3 resulted to 1, because we have only one such something that passed the test.
If we modify the string to have two ‘all’s’ as follows:

const str = "Is this all ? Find all more";

Output 3 will now have 2 as its result.

I hope all these were clearer. I felt more explanation was needed.
If there was anything you found out to be wrong, please let me know.

1 Like

thank you for this help

1 Like

I’m glad you found it helpful. :relieved: