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

491
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

0

Managed to figure it out, this works as expected:

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

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