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