During the November edition of our Champion Hour - ScriptRunner series, a ScriptRunner user asked us this question:
We keep having team names changed, which means we need to replace the team names on Confluence pages. How would you go about a find and replace Scriptrunner script that finds and replaces text within a Confluence page body?
Below you will find guidance on how to find and replace text across several Confluence pages at once using a ScriptRunner script. This post includes a step by step video demo and the groovy code. Feel free to download it and use it for your own needs.
I will begin by pointing out that Confluence has a native find and replace feature (look for the magnifying glass at the right of the editor-panel), but it is pretty limited since it’s only for finding and replacing text on a single page.
Imagine a space which has pages that contain tables with department names in their headers. A Confluence admin wants to abbreviate every occurrence of “Human Resources” in all table headers to “HR”. The script below will only change the text “Human Resources” to “HR” when it is found in a table header, and leaves all other occurrences of “Human Resources” as they are. You can always make this more general and choose to simply replace all occurrences of whatever text you want to replace in a page.
For a one time use, run this script in ScriptRunner’s Script Console. This script was written with a one-time use in mind, but it can also be adapted to work with some of our built-in scripts or other ScriptRunner features, such as the CQL Service Escalation job.
You could adjust this script to fit a wide variety of use cases, such as finding and replacing text on every page in a particular space, or any page that meets some other criteria. Additionally, as in this example, you may only want to replace particular occurrences of that text within a page. We hope that helps!
Try ScriptRunner for free for 30 days
ScriptRunner for Confluence is an amazingly versatile tool that lets you customise, extend and automate Confluence as much as you need. Explore what you can achieve with a 30-day free trial: