Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

501
Views
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 answers
Answer question

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 Report

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 Report

0

Managed to figure it out, this works as expected:

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!