Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

100
Visualizações
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/'));

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Testing whether a string matches a regular expression will test whether any individual position in the string matches the regular expression. For example

/x/

matches

'fooxbar'

starting at index 3 of the string.

Your

/(?!.*investing)(?!.*news)/

will match all strings - namely, at the first position after which neither investing nor news exist. If both substrings are not in the target string, this will be at the start of the string. Otherwise, if both substrings this will be the position just past where the last one of them starts. For example, against:

https://br.investing.com/news/

it will match at this position:

https://br.investing.com/news/
                          ^

because starting at the e of news, it's true that neither investing nor news exist.

If you want to fix it, you can require the match to start at the beginning of the string, so that the lookaheads span the whole length of the string.

const negativeTest = x => (/^(?!.*investing)(?!.*news)/).test(x);
//                          ^ use the ^ anchor
//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/'));
console.log(negativeTest('foobar'));

If you want the pattern to check that both do not exist, but one or the other is OK, then you'll need to alternate: match (negatively) the first phrase followed by the second, or the second followed by the first.

const negativeTest = x => (/^(?!.*investing.*news|.*news.*investing)/).test(x);
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/'));
console.log(negativeTest('foobar'));

about 4 years ago · Juan Pablo Isaza Relatório

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda