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

96
Visualizações
Javascript replace string with list from array

I have a string that is a normal sentence. I need to replace the characters in the string if they are found in a given array. For example,

const arr = ["(model: Audi)", "(model: Kia)"];

if the string is:

"How is your (model: Audi) today?";

The result should be "How is your Audi today?".

Is there a way to do this with regex? I read somewhere that regex has better performance. I've tried looping thru the array then replacing the characters but I couldnt get it working and my solution would have nested loops due to the given string

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

const arr = ["(model: Audi)", "(model: Kia)"];
let string = "How is your (model: Audi) today?";
for (let data of arr){
  string = string.replace(data, data.split(": ")[1].replace(")",""))
}
console.log(string);

about 4 years ago · Juan Pablo Isaza Relatório

0

Well, this is the simplest I have managed to do.

However, I myself kind of consider it a bad solution.

Let me know if it helps at all...

const myString = "How is your (model: Audi) today?";
const arr = ["(model: Audi)", "(model: Kia)"];

arr.forEach(item => {
  const part = item.match(/\w+\)$/)[0];
  const subPart = part.substring(0, part.length - 1);

  if (myString.includes(item))
    console.log(myString.replace(item, subPart));
});

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