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

215
Visualizações
Can regexp avoid a pattern in JavaScript?

I have an expression in JS to match all the words containing (for example) 'foo+', but I would like to discard those of them who end with some other pattern.

Is there a way to make this in only one regular expression?

example:

myExp = /foo+\w*/; /*should add something to avoid ending with 'ba+r'*/

myExp.matchAll('boo, fo, food, foobaaar, foobr');

should result in ["food", "foobr"] not including 'foobaaar'

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

0

You want to use a negative lookahead like (?!not_this), of course replace not_this with the appropriate pattern to exclude using word-break characters \b, or whatever fits your logic.

let myExp = /\bfoo+(?!\w*ba+r\b)\w*/g;

console.log(...' boo fo food foobaaar foobr foobaardd boofoo foodbar '.matchAll(myExp));

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use negative look ahead. To exclude cases where ba+r is not the ending, use \b in that look ahead.

let myExp = /foo+(?!\w*ba+r\b)\w*/g;
let input = 'boo, fo, food, foobaaar, foobr, foobaardd';

console.log(...input.match(myExp));

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