ReplaceTextSignature
ReplaceTextSignature = (
searchFor,replace) =>number
Function signature for replacing text content within a document element.
Parameters
Section titled “Parameters”searchFor
Section titled “searchFor”The pattern to search for. Can be:
- A string literal (case-sensitive, will match exact occurrences)
- A RegExp for pattern matching (e.g.,
/\bword\b/ifor case-insensitive word boundaries)
replace
Section titled “replace”The replacement content. Can be:
- A string: Simple text replacement
- A
Replacementobject: Replace text and/or apply formatting - A
ReplaceFncallback: Dynamic replacement based on matched text
Returns
Section titled “Returns”The total number of replacements made
Remarks
Section titled “Remarks”The replaceText method searches for all occurrences of a specified pattern and replaces them
with new content. It can operate on various document elements including paragraphs, tables,
sections, and the entire document body. The method processes all matches sequentially and
returns the total number of replacements made.
Examples
Section titled “Examples”// Replace all occurrences of "hello" with "goodbye"const count = paragraph.replaceText("hello", "goodbye");console.log(`Replaced ${count} occurrences`);// Find all "hi" and highlight themdraft.replaceText(/\\bhi\\b/i, { text: "REPLACED", formatting: { highlight: "#ff0000", bold: true }});// Convert all numbers to their doubled valuedraft.replaceText(/\d+/g, (match) => { const num = parseInt(match); return { text: (num * 2).toString(), formatting: { color: "#0000ff" } };});// Highlight all occurrences of "important" without changing the textdraft.replaceText(/important/i, (match) => ({ text: match, // Keep original text (preserves case) formatting: { highlight: "#ffff00", bold: true }}));