Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!