I have seen similar questions to this but none that are trying to achieve quite the same thing. I need to find every instance of a regex pattern but replace every instance with a different value. Here is my code:
function replacingText(){
var names = ["Ethan", "Kyle", "Chase", "Cole"];
var sentance = 'This is [Cole] and [Chase].'
var regex = /\[(.*?)\]/gm;
for(i of names){
sentance = sentance.replace(regex, i);
}
console.log(sentance);
}
This code results in this:
This is Ethan and Ethan.
But I want:
This is Ethan and Kyle.
Really, I just need some way to find each item that is inside of brackets [ ] and replace that item with a unique value and then rebuild the string with the new values. I am not partial to any approach.
[Cole],[Chase]) than names?