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

144
Views
replace word with match from object in JS

I am trying to replace words in a string with matches from an object. If a word matches the property from an object, it will be replaced by the relevant value. My problem is cases where there is a character before and after the word that should be replaced, unless the character is a whitespace or a hyphen.

function fixTypos(str) {
  var typoObject = {
   descriptiogn:'description',
   decscription:'description',
   vdescription:'description',
   wdescription:'description',
   descriptiog:'description',
   statucs:'status',
   statuqs:'status',
   cstatus:'status',

  for (var key in typoObject) {
    str = str.replace(new RegExp(`\\b${key}\\b`, "gi"), typoObject[key]);
   }
  return str; 
} 

teststring: 'word -decscription word2 adescriptiogn word3 -astatucs'

current output: 'word -description word2 adescriptiogn word3 -astatucs'

desired output: 'word -description word2 description word3 -status'

My approach might be the wrong one, since I start to doubt it can be done via regex, but maybe someone here has an idea for me?

Edit: added more variety in the object. The object is an example, but the one I use for my project contains over 2k property:value pairs with not always matching values

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could build one regular expression to catch any of the keywords, using a capture group to identify which it was, and a callback function to do the lookup for the translation:

const translation = {
    descriptiogn:'description',
    decscription:'description',
    vdescription:'description',
    wdescription:'description',
    descriptiog:'description',
    statucs:'status',
    statuqs:'status',
    cstatus:'status',
};
const regex = new RegExp("\\b\\w*(" 
    + Object.keys(translation)
            .sort((a, b) => b.length - a.length)
            .join("|") 
    + ")\\w*\\b", "g");

const fixTypos = str => str.replace(regex, (_, match) => translation[match]);

const teststring= 'word -decscription word2 adescriptiogn word3 -astatucs'

console.log(fixTypos(teststring));

Sorting the keywords from longest to shortest may be necessary so to give precedence to the longer matches when also a shorter key would match.

about 4 years ago · Juan Pablo Isaza Report

0

I would just use an alternation here. Create an array of description variant terms to find, and then do a global replacement.

var input = 'word -decscription word2 adescriptiogn word3 -adescriptiogn';
var terms = ['descriptiogn', 'decscription', 'vdescription', 'wdescription', 'descriptiog'];
var regex = new RegExp("\\b\\w*(?:" + terms.join("|") + ")\w*\\b", "g");
var output = input.replace(regex, "description");
console.log(input);
console.log(output);

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!