The values in my spreadsheet are these:
/2021/
/20212022/
/2022/
/20222023/
/2023/
To try to find these values in a string and then replace with /@@@@/
I tried creating a map:
var sheet = SpreadsheetApp.getActive().getSheetByName('One')
var liga = 'https://int.testestest.com/national/united-states/mls/2022/regular-season/r66725/'
var years = sheet.getRange('One!A10:A14').getValues().flat();
var mapObj = {one: years[0],two: years[1],three: years[2],four: years[3],five: years[4]};
var liga_substitute = liga.replace(/\b(?:one|two|three|four|five)\b/gi, matched => mapObj[matched]);
var liga_final_subs = liga_substitute.replace('one','/@@@@/').replace('two','/@@@@/').replace('three','/@@@@/').replace('four','/@@@@/').replace('five','/@@@@/')
But the result after substitution remains the same:
https://int.testestest.com/national/united-states/mls/2022/regular-season/r66725/
My final expected result is:
https://int.testestest.com/national/united-states/mls/@@@@/regular-season/r66725/
And in var liga_final_subs =
exist the risk of finding these parts of text in the wrong places and replacing what shouldn't.
How should I go about doing this correctly?
CodePudding user response:
Not sure what you trying to gain. Here is the code that does the job (I hope):
var liga = 'https://int.testestest.com/national/united-states/mls/2022/regular-season/r66725/'
var years = [
'/2021/',
'/20212022/',
'/2022/',
'/20222023/',
'/2023/',
];
var liga_final_subs = liga;
years.forEach(x => liga_final_subs = liga_final_subs.replace(x, '/@@@@/'));
console.log(liga_final_subs);