Skip to content
Document Authoring DA  API Docs v1.10.0
npmGitHub

ReplaceTextSignature

ReplaceTextSignature = (searchFor, replace) => number

Function signature for replacing text content within a document element.

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/i for case-insensitive word boundaries)

ReplacementOrReplaceFn

The replacement content. Can be:

  • A string: Simple text replacement
  • A Replacement object: Replace text and/or apply formatting
  • A ReplaceFn callback: Dynamic replacement based on matched text

number

The total number of replacements made

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.

// Replace all occurrences of "hello" with "goodbye"
const count = paragraph.replaceText("hello", "goodbye");
console.log(`Replaced ${count} occurrences`);
// Find all "hi" and highlight them
draft.replaceText(/\\bhi\\b/i, {
text: "REPLACED",
formatting: {
highlight: "#ff0000",
bold: true
}
});
// Convert all numbers to their doubled value
draft.replaceText(/\d+/g, (match) => {
const num = parseInt(match);
return {
text: (num * 2).toString(),
formatting: { color: "#0000ff" }
};
});
// Highlight all occurrences of "important" without changing the text
draft.replaceText(/important/i, (match) => ({
text: match, // Keep original text (preserves case)
formatting: { highlight: "#ffff00", bold: true }
}));