I have a script in Google sheets
I am trying to find and replace headers on a sheet from a table of values on a different sheet
It is mostly working as desired but the replace
is not working for any string that ends in ?
I do not know in advance when a ?
will be present
I am using this:
const regex = new RegExp("(?<![^|])(?:" + search_for.join("|") + ")(?![^|])", "g");
I have tried to figure out how to correct my Regex but not getting it
Thanks in advance for your assistance with this
I have in a sheet:
search_for | replace_with |
---|---|
ABC Joe | MNQ |
XYZ car | NNN XXX |
DDD foo? | Bob bar |
I have for Headers on a different sheet:
Label | Id | ABC Joe | XYZ car | DDD foo? |
---|
after running the replacement I want for headers:
Label | Id | MNQ | NNN XXX | Bob bar |
---|
what I get is:
Label | Id | MNQ | NNN XXX | DDD foo? |
---|
var data = range.getValues();
search_for.forEach(function(item, i) {
pair[item] = replace_with[i];
});
const regex = new RegExp("(?<![^|])(?:" + search_for.join("|") + ")(?![^|])", "g");
//Update Header row
//replace(/^\s+|\s+$|\s+(?=\s)/g, "") - Remove all multiple white-spaces and replaces with a single WS & trim
for(var m = 0; m<= data[0].length - 1; m++){
data[0][m] = data[0][m].replace(/^\s+|\s+$|\s+(?=\s)/g, "").replace(regex,(m) => pair[m])
}