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

155
Vistas
Replace contant of an array in a string with a text-JavaScript
let message ="hello https://ggogle.com parul https://yahoo.com and http://web.com";
let url=["https://ggogle.com", "https://yahoo.com", "http://web.com"];

I want to put text "***" in place of all url found in the message array from url array.

Tried this way -

let message = "hello https://ggogle.com parul https://yahoo.com and http://web.com";
let url = ["https://ggogle.com", "https://yahoo.com", "http://web.com"];
const replace = url.map(item => {
  let indexes = message.indexOf(item);
  let output = message.replaceAt(indexes, "***")
})

Error-message.replace is not a function,also if the logic is correct? Thanks!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

1) You can easily achive the result using reduce

let message =
  "hello https://ggogle.com parul https://yahoo.com and http://web.com";

let url = ["https://ggogle.com", "https://yahoo.com", "http://web.com"];

const result = url.reduce((msg, str) => msg.replace(new RegExp(str, "g"), "***"), message);

console.log(result);

2) You can also achieve the solution with forEach loop

let message =
  "hello https://ggogle.com parul https://yahoo.com and http://web.com";

let url = ["https://ggogle.com", "https://yahoo.com", "http://web.com"];

url.forEach((s) => (message = message.replace(new RegExp(s, "g"), "***")));

console.log(message);

3) Using replace

let message =
  "hello https://ggogle.com parul https://yahoo.com and http://web.com";

let url = ["https://ggogle.com", "https://yahoo.com", "http://web.com"];

const result = url.reduce((msg, str) => {
  while (msg.indexOf(str) !== -1) msg = msg.replace(str, "***");
  return msg;
}, message);

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

The error is correct. replaceAt isn't a function, you're gonna have to use something like string substr() and then add your string inside, unless you think the string replace() method is good for your situation. I don't know enough about your case to decide.

This question might be useful, but make sure you actually add in the "***" after you cut out part of the string.

about 4 years ago · Juan Pablo Isaza Denunciar

0

If I understood you correctly, you can just use replace instead of replaceAt. For example:

let message = "hello https://ggogle.com parul https://yahoo.com and http://web.com";
let url = ["https://ggogle.com", "https://yahoo.com", "http://web.com"];
const replace = url.map(item => {
    message = message.replace(item, "***")
})

console.log(message);

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