Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

94
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda