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

497
Vistas
Regex match either word, but the exact word

i'm struggling figure out a regex. I want to replace a space with a dash but only if the following word matches either class or series.

I have tried the following:

/\s(?=[^\s]*(class|series)$)/gi

'  E-Class' => ' -E-Class'
'E Class'   => 'E-Class'
' E Class'  => ' E-Class'

Above is close, but if there is already a dash it puts one before, which it shouldn't.

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

0

Following regex/approach matches the criteria described by the OP ...

/\s(?=class|series)|\s(\w+-?)+(?=class|series)/gi.

Actually it's two separate regular expressions, one (\s(?=class|series)) for the obviously simple case of matching the pattern of e.g. ' E Class', the other one (\s(\w+-?)+(?=class|series)) for the more complex patterns such as e.g. ' E-Class' or ' Ee-ef-fooSeries' where the OP wants to "... replace a space with a dash but only if the following word [contains] either class or series" (contains and not matches; see my first comment above).

The regex' match result needs to be handled by a custom replacer function. A test case might look similar to the next provided one ...

const testEntries = [

  // OP's request.
  ['  E-Class', ' -E-Class'],
  ['E Class', 'E-Class'],
  [' E Class', ' E-Class'],

  // Bonus test.
  ['  Ee-eClass', ' -Ee-eClass'],
  [' Ee-ef-Class', '-Ee-ef-Class'],
  ['  Ee-ef-fooSeries', ' -Ee-ef-fooSeries'],
];
const regX = (/\s(?=class|series)|\s(\w+-?)+(?=class|series)/gi);

function didPassTest([value, expectedValue]) {
  return (
    value.replace(regX, (match) =>
      '-' + match.trim()
    ) === expectedValue
  );
} 
console.log(
  'testEntries.every(didPassTest) ?..',
  testEntries.every(didPassTest)
);

One can unify the above OR based regex literal into a version which matches the overall pattern while capturing the word ...

/\s((?:\w+-?)*(?:class|series))/gi

The above example code then changes to ...

const testEntries = [

  // OP's request.
  ['  E-Class', ' -E-Class'],
  ['E Class', 'E-Class'],
  [' E Class', ' E-Class'],

  // Bonus test.
  ['  Ee-eClass', ' -Ee-eClass'],
  [' Ee-ef-Class', '-Ee-ef-Class'],
  ['  Ee-ef-fooSeries', ' -Ee-ef-fooSeries'],
];
const regX = (/\s((?:\w+-?)*(?:class|series))/gi);

function didPassTest([value, expectedValue]) {
  return (value.replace(

    regX, (match, word) => '-' + word

  ) === expectedValue);
} 
console.log(
  'testEntries.every(didPassTest) ?..',
  testEntries.every(didPassTest)
);

about 4 years ago · Juan Pablo Isaza Denunciar

0

I believe you should only match the space before class or series and not every space before so you can try something like this :

/\s(class|series)$/gi
about 4 years ago · Juan Pablo Isaza Denunciar

0

Managed to figure it out, this works as expected:

/\s(?=class|series)/gi

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