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

276
Visualizações
Replace words from a string from a wordlist in JavaScript

I want to do it like I give a list of words and a meaning to them e.x.:

'szia' = 'hello' 'mizu' = 'whats up'

and then I have a string that goes like this: var string = "szia mizu". How can I replace the words there from the list?

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

let sentence = "szia mizu";

const traduction= {
  "hello": ['szia'],
  "whats up": ['mizu']
};

function fileAndReplace(sentence, traduction) {
  let newSentence = sentence;
    
  Object.keys(traduction).forEach(key => {
      const checktrad = new RegExp(traduction[key].join('|'),'gi');
      newSentence = newSentence.replace(checktrad , key);
  })
     
  return newSentence;
}

console.log(fileAndReplace(sentence, traduction))

you need to iterate through all the keys in your dictionary and create a regex for that specific key. Then you just feed that regex to the .replace() with the synonym regex and the key you want to replace it with.

about 4 years ago · Santiago Gelvez Relatório

0

You can archive this with js replace() function:

const needles = {
  szia: "hello",
  mizu: "whats up"
};

let str = "szia mizu";

Object.keys(needles).forEach(n => {
  str = str.replace(n, needles[n]);  
})

console.log(str)

about 4 years ago · Santiago Gelvez 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