I am trying to add variables to my function call
The original function came from here Find and replace nth occurrence of [bracketed] expression in string
var s = "HELLO, WORLD!";
var nth = 0;
s = s.replace(/L/g, function (match, i, original) {
nth++;
return (nth === 2) ? "M" : match;
});
alert(s); // "HELMO, WORLD!";
I am trying to do this
function ReplaceNth_n() {
Logger.log(ReplaceNth("HELLO, WORLD!", "L", "M"))
}
function ReplaceNth(strSearch,search_for, replace_with) {
var nth = 0;
strSearch = strSearch.replace(/search_for/g, function (match, i, original) {
nth++;
return (nth === 2) ? replace_with : match;
});
return strSearch
}
This part is failing; Replacing
s = s.replace(/L/g, function (match, i, original)
with
strSearch = strSearch.replace(/search_for
/g, function (match, i, original)
I have tried variations on
strSearch = strSearch.replace('/'+ search_for +'/g', function (match, i, original)
But not getting how to do this
Thanks