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.