In your script, replace('/.*example.*/', 'example2') is required to be modified as follows.
From
data[row][col] = (data[row][col]).toString().replace('/.*example.*/', 'example2');
To:
data[row][col] = (data[row][col]).toString().replace('example', 'example2');
or
data[row][col] = (data[row][col]).toString().replace(/example.com\//, 'example2.com/');
By this, example.com/this-is-a-link is modified to example2.com/this-is-a-link.
In your situation, as other method, how about using TextFinder? When your script is modified, it becomes as follows. When TextFinder is used, I think that the process cost will be lower than that of current script.
Modified script:
function rePlace() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("a1:a10000");
range.createTextFinder("example").replaceAllWith("example2"); // Added
}
or
function rePlace() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("a1:a10000");
range.createTextFinder("example.com/").useRegularExpression(true).replaceAllWith("example2.com/"); // Added
}
References:
Added 1:
When you want to use above script for several sheets in the active Google Spreadsheet, how about the following script?
Sample script:
In this script, the range of a1:a10000 in all sheets is used.
SpreadsheetApp
.getActiveSpreadsheet()
.getSheets()
.forEach(sheet => sheet
.getRange("a1:a10000")
.createTextFinder("example.com/")
.useRegularExpression(true)
.replaceAllWith("example2.com/")
);
When you want to use the specific sheets, you can also use the following script.
var sheetNames = ["Sheet1","Sheet3",,,]; // Please set the sheet names.
SpreadsheetApp
.getActiveSpreadsheet()
.getSheets()
.forEach(sheet => {
if (sheetNames.includes(sheet.getSheetName())) {
sheet
.getRange("a1:a10000")
.createTextFinder("example.com/")
.useRegularExpression(true)
.replaceAllWith("example2.com/")
}
});
Added 2:
About your 2nd question of Is there a way to replace all at once without the script visiting one sheet after another to make replacements? Because I think that's why it's delaying. I have 50 sheets. Perfect solution but if it would mass replace I think it would be even better? from your comment, in this case, I would like to propose to use Sheets API. When Sheets API is used, I think that the process cost will be able to be reduced.
Sample script:
Before you use this script, please enable Sheets API at Advanced Google services.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const requests = ss.getSheets().map(s => ({
findReplace:{
find: "example.com/",
searchByRegex: true,
replacement: "example2.com/",
range: {sheetId: s.getSheetId(), startRowIndex: 0, startColumnIndex: 0, endColumnIndex: 1}
}
}));
Sheets.Spreadsheets.batchUpdate({requests: requests}, ss.getId());
Note:
If you will check all cells in all sheets in a Google Spreadsheet, I think that you can also use the following script.
SpreadsheetApp.getActiveSpreadsheet().createTextFinder("example.com/").useRegularExpression(true).replaceAllWith("example2.com/");