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

202
Vistas
replace multiple words in string based on an array

I didn't find my answer in old StackOverflow questions, so I have an array that contains some words, also I have a long string that has the same word as in the array.

Now, I want to add a prefix to words that has $, for example, I want to change all $status to $prefix_status

This is what I test, but replaceAll doesn't replace anything.

const myPrefix = 'prefix'
const array = ['status', 'id', 'offset', 'limit'];
const string = '($status: CardStatus, $id: ID, $offset: Int, $limit: Int), (status: $status, id: $id, offset: $offset, limit: $limit)';

array.forEach((i) => {  
  string.replaceAll(`$${i}:`, `$${myPrefix}_${i}:`)
})

NOTE: if I write replaceAll('$status:', `$${myPrefix}_${i}:`) it works, but it seem replaceAll doesn't accept variables as its first argument.

about 4 years ago · Juan Pablo Isaza
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