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

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

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

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 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