Hey, friends today I will teach you how to Find and Replace Text in Google Docs with RegEx Search Patterns. The Google Apps Script DocumentApp service makes it simple to search for and replace text in Google Docs. To find text elements in a document that match a pattern and replace them with the specified text, use the findText method with simple regular expressions.. so let get started with today Code snippets. Getting different problems is altogether gives a very different experience. today the Code snippets I am going to share with you is How to Find and Replace Text in Google Docs with RegEx Search Patterns.
Here’s a simple code sample that replaces the first occurrence of “GSuite” in the active Google Document with “Google Workspace.”
You might also like our trending code snippets
- How to Delete Blank Rows from Tables in your Google Documents
- How to Replace Accented Characters (diacritics) with English letters in Google Sheets
- Exceeded maximum execution time Exception in Google Apps Script
- Create a Telegram Bot for Sending Notifications using Google Apps Script
const searchAndReplaceInGoogleDocs = () => {
const searchText = "GSuite";
const replaceText = "Google Workspace";
const document = DocumentApp.getActiveDocument();
const documentBody = document.getBody();
const searchResult = documentBody.findText(searchText);
if (searchResult !== null) {
const startIndex = searchResult.getStartOffset();
const endIndex = searchResult.getEndOffsetInclusive();
const textElement = searchResult.getElement().asText();
textElement.deleteText(startIndex, endIndex);
textElement.insertText(startIndex, replaceText);
}
document.saveAndClose();
};
This is all well and good, but in some cases, if the search text does not transform into a valid regular expression, the simple search and replace function may fail.
For example, if you have a text block in the document called Hello (World) that you want to replace with Hello World, the above snippet will fail with an error message that says Exception: Invalid regular expression pattern.
To work around the issue, replace all of the special characters in the search pattern that have a special meaning in the RegEx world. Characters such as hyphens, brackets, question marks, and the plus symbol are examples of these.
Our reworked search and replace function would then be as follows:
const escapeRegex = (str) => str.replace(/[-[\]/{}()*+?.\\^$|#]/g, "\\$&");
const searchAndReplaceInGoogleDocs = () => {
const searchText = "Hello (World";
const replaceText = "Hello World";
const document = DocumentApp.getActiveDocument();
const documentBody = document.getBody();
const searchResult = documentBody.findText(escapeRegex(searchText));
if (searchResult !== null) {
const startIndex = searchResult.getStartOffset();
const endIndex = searchResult.getEndOffsetInclusive();
const textElement = searchResult.getElement().asText();
textElement.deleteText(startIndex, endIndex);
textElement.insertText(startIndex, replaceText);
}
document.saveAndClose();
};