I wrote a loop to split a string and search for certain text to replace it with another string. Here is an example of what I have done:
function replaceStr(str, find, replace) {
for (var i = 0; i < find.length; i++) {
str = str.replace(new RegExp(find[i], 'gi'), replace[i]);
}
return str;
}
var str = 'some text contain car and some house contain car, or car contain someone';
var values = "cat,dog,chicken";
splt = values.split(',');
for (i = 0; i < splt.length; i++) {
var find = ['car'];
var replace = ['' + values[i] + ''];
replaced = replaceStr(str, find, replace);
}
console.log(replaced);
//console.log(splt.length);
However, the result is returning zeros.
I am trying to locate all instances of "car" in the text and replace them with strings that were separated by commas. Can someone assist me with this issue, please?