Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda