Skip to content
Advertisement

Divergence with lookahead RegExp

I’m doing a test where all links that don’t have ‘investing’ and ‘news’ are rejected, but I’m not understanding the logic of ?= and ?!, and I believe I’m doing something wrong since the opposite of the logic below is not being matched. Could someone give me a light?

Note: I could just use !, but I would like to understand what the error of this expression is.

const positiveTest = x => (/(?=.*investing)(?=.*news)/).test(x);
const negativeTest = x => (/(?!.*investing)(?!.*news)/).test(x);

//if 'investing' and 'news' are found
console.log('positiveTest:')
console.log(positiveTest('https://br.investing.com/news/'));
console.log(positiveTest('https://br.investing.com/nws/'));
console.log(positiveTest('https://br.inveting.com/news/'));

//if 'investing' and 'news' are not found
console.log('negativeTest:')
console.log(negativeTest('https://br.investing.com/news/'));
console.log(negativeTest('https://br.investing.com/nws/'));
console.log(negativeTest('https://br.inveting.com/news/'));

Advertisement

Answer

a while ago I understood the reason for this expression is wrong,

Lookahead/Lookbehind needs a reference to search, and if you don’t put(or have) a reference, it will tests each index of the string like a .(?=)/.(?!). Being so, the boundary expression ^ and .* is necessary at the beginning to prevent that lookahead tests each index of the string.

a more efficient way of writing this generalized positive lookahead is (?=investing|news), but generalized negative lookahead is not viable because it requires more expressions (Ex: ^(?=.*(?:investing|news))) It is more viable and efficient to invert a positive lookahead with the NOT ! operator.

Advertisement