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

212
Vistas
Javascript Regex to get list of any words except a specific word but also get that specific word in another group

I want to get all categories listed in example below fruit and meat but not word mandatory but I want to still get mandatory if present :

categories fruit meat mandatory

I tried to inspire from Match everything except for specified strings

https://regex101.com/r/peXJXx/1

/(categories )(^(?!.+(mandatory)))(mandatory)?/gis

But can't get it work

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

const r = 'categories fruit meat mandatory';
const [, ...categories] = r.split(' ').filter(_ => _ !== 'mandatory');

You don't need Regex for such a problem, I used split(' ') to split the string into words (convert it to an array), then filter them from 'mandatory', I used also destructuring to remove the first element.

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you insist on using regex, this might be what you want.

const input = "categories fruit meat mandatory";
const regex = /\w+/gm;
const regex2 = /^\w+/g;

const results = input.matchAll(regex);

let type = input.match(regex2)[0];

let isMandatory = false;
let isCategory = false;
let categories = [];
for (const result of results){
    switch(result[0]){
    case "categories": isCategory = true; break;
    case "mandatory": isMandatory = true; break;
    default: categories.push(result[0]);
  }
}
console.log("Type: " + type);
console.log("Categories: " + categories.join(", "));
console.log("Mandatory: " + (isMandatory ? "Yes" : "No"));
Type: categories
Categories: fruit, meat
Mandatory: Yes

You can spruce it up however you want. Essentially if you have multiple inputs, you'd loop over each string and run this code. If you have other types instead of category, you can add something like isDepartment = false and then in the switch do case "departments": isDepartment = true; break;.

You could also just modify the regex to ignore the first word and create a second regex pattern to identify what the first word is specifically like const regex2 = /^\w+/g; and then do let type = input.match(regex2)[0];.

Here's a live sample https://jsfiddle.net/gey3oqLp/1/

Edit: Actually now that I think about it. If we're going the switch route, we don't need to match anything specific in the regex. We only need to match each word and deal with it later down the road. I've removed updated the regex to the simpler version.

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