You can provide a CQL (Confluence Query Language) query to search for pages. Your JavaScript code can then return a modified version of found pages. This allows you to implement search and replace functionality with whatever complexity you need.
The following example shows how to find all pages that contain the text Houston, we have a problem.:
Each of the found pages is then passed to the JavaScript function process. If, and only if, the text Houston, we have a problem. is part of the first paragraph, the text is extended with This is Houston. Say again, please. otherwise, the page is unchanged:
function process(page) {
const body = JSON.parse(page.body.atlas_doc_format.value);
if (body.content.length == 0)
return null;
if (body.content[0].type !== "paragraph")
return null;
if (body.content[0].content.length == 0)
return null;
if (body.content[0].content[0].text !== "Houston, we have a problem.")
return null;
body.content[0].content[0].text =
body.content[0].content[0].text + " This is Houston. Say again, please."
page.body.atlas_doc_format.value = JSON.stringify(body);
page.version.number = page.version.number + 1;
page.version.message = "Search And Replace";
return page;
}