Find and Replace Text in Documents

Contents
[ ]

Use one of the Replace methods to find or replace a particular string within the current range. It returns the number of replacements made, so it is useful for searching strings without replace. An exception is thrown if a captured or replacement string contains one or more special characters: paragraph break, cell break, section break, field start, field separator, field end, inline picture, drawing object, footnote.

The following code example shows how to find some text and replace it:

// Replaces all 'sad' and 'mad' occurrences with 'bad'
doc.getRange().replace("sad", "bad", false, true);

// Replaces all 'sad' and 'mad' occurrences with 'bad'
doc.getRange().replace(Pattern.compile("[s|m]ad"), "bad");

See also: