replace method

replace(pattern, replacement)

Replaces all occurrences of a specified character string pattern with a replacement string.

replace(pattern: string, replacement: string)
ParameterTypeDescription
patternstringA string to be replaced.
replacementstringA string to replace all occurrences of pattern.

Remarks

The pattern will not be used as regular expression. Please use Aspose.Words.Range.Replace(System.Text.RegularExpressions.Regex,System.String) if you need regular expressions.

Used case-insensitive comparison.

Method is able to process breaks in both pattern and replacement strings.

You should use special meta-characters if you need to work with breaks:

  • &p - paragraph break

  • &b - section break

  • &m - page break

  • &l - manual line break

Use methodRange.replace() to have more flexible customization.

Returns

The number of replacements made.

replace(pattern, replacement, options)

Replaces all occurrences of a specified character string pattern with a replacement string.

replace(pattern: string, replacement: string, options: Aspose.Words.Replacing.FindReplaceOptions)
ParameterTypeDescription
patternstringA string to be replaced.
replacementstringA string to replace all occurrences of pattern.
optionsFindReplaceOptionsFindReplaceOptions object to specify additional options.

Remarks

The pattern will not be used as regular expression. Please use Aspose.Words.Range.Replace(System.Text.RegularExpressions.Regex,System.String,Aspose.Words.Replacing.FindReplaceOptions) if you need regular expressions.

Method is able to process breaks in both pattern and replacement strings.

You should use special meta-characters if you need to work with breaks:

  • &p - paragraph break

  • &b - section break

  • &m - page break

  • &l - manual line break

  • && - & character

Returns

The number of replacements made.

Examples

Shows how to perform a find-and-replace text operation on the contents of a document.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

builder.writeln("Greetings, _FullName_!");

// Perform a find-and-replace operation on our document's contents and verify the number of replacements that took place.
let replacementCount = doc.range.replace("_FullName_", "John Doe");

expect(replacementCount).toEqual(1);
expect(doc.getText().trim()).toEqual("Greetings, John Doe!");

Shows how to add formatting to paragraphs in which a find-and-replace operation has found matches.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

builder.writeln("Every paragraph that ends with a full stop like this one will be right aligned.");
builder.writeln("This one will not!");
builder.write("This one also will.");

let paragraphs = doc.firstSection.body.paragraphs.toArray();

expect(paragraphs.at(0).paragraphFormat.alignment).toEqual(aw.ParagraphAlignment.Left);
expect(paragraphs.at(1).paragraphFormat.alignment).toEqual(aw.ParagraphAlignment.Left);
expect(paragraphs.at(2).paragraphFormat.alignment).toEqual(aw.ParagraphAlignment.Left);

// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
let options = new aw.Replacing.FindReplaceOptions();

// Set the "Alignment" property to "ParagraphAlignment.Right" to right-align every paragraph
// that contains a match that the find-and-replace operation finds.
options.applyParagraphFormat.alignment = aw.ParagraphAlignment.Right;

// Replace every full stop that is right before a paragraph break with an exclamation point.
var count = doc.range.replace(".&p", "!&p", options);

expect(count).toEqual(2);
paragraphs = doc.firstSection.body.paragraphs.toArray();
expect(paragraphs.at(0).paragraphFormat.alignment).toEqual(aw.ParagraphAlignment.Right);
expect(paragraphs.at(1).paragraphFormat.alignment).toEqual(aw.ParagraphAlignment.Left);
expect(paragraphs.at(2).paragraphFormat.alignment).toEqual(aw.ParagraphAlignment.Right);
expect(doc.getText().trim()).toEqual("Every paragraph that ends with a full stop like this one will be right aligned!\r" +
                        "This one will not!\r" +
                        "This one also will!");

Shows how to replace text in a document’s footer.

let doc = new aw.Document(base.myDir + "Footer.docx");

let headersFooters = doc.firstSection.headersFooters;
let footer = headersFooters.getByHeaderFooterType(aw.HeaderFooterType.FooterPrimary);

let options = new aw.Replacing.FindReplaceOptions();
options.matchCase = false;
options.findWholeWordsOnly = false;

let currentYear = new Date().getYear();
footer.range.replace("(C) 2006 Aspose Pty Ltd.", `Copyright (C) ${currentYear} by Aspose Pty Ltd.`, options);

doc.save(base.artifactsDir + "HeaderFooter.ReplaceText.docx");

Shows how to replace all instances of String of text in a table and cell.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

let table = builder.startTable();
builder.insertCell();
builder.write("Carrots");
builder.insertCell();
builder.write("50");
builder.endRow();
builder.insertCell();
builder.write("Potatoes");
builder.insertCell();
builder.write("50");
builder.endTable();

let options = new aw.Replacing.FindReplaceOptions();
options.matchCase = true;
options.findWholeWordsOnly = true;

// Perform a find-and-replace operation on an entire table.
table.range.replace("Carrots", "Eggs", options);

// Perform a find-and-replace operation on the last cell of the last row of the table.
table.lastRow.lastCell.range.replace("50", "20", options);

expect(table.getText().trim()).toEqual("Eggs\u000750\u0007\u0007" +
                        "Potatoes\u000720\u0007\u0007");

Shows how to toggle case sensitivity when performing a find-and-replace operation.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

builder.writeln("Ruby bought a ruby necklace.");

// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
let options = new aw.Replacing.FindReplaceOptions();

// Set the "MatchCase" flag to "true" to apply case sensitivity while finding strings to replace.
// Set the "MatchCase" flag to "false" to ignore character case while searching for text to replace.
options.matchCase = matchCase;

doc.range.replace("Ruby", "Jade", options);

expect(doc.getText().trim()).toEqual(matchCase ? "Jade bought a ruby necklace." : "Jade bought a Jade necklace.");

Shows how to toggle standalone word-only find-and-replace operations.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

builder.writeln("Jackson will meet you in Jacksonville.");

// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
let options = new aw.Replacing.FindReplaceOptions();

// Set the "FindWholeWordsOnly" flag to "true" to replace the found text if it is not a part of another word.
// Set the "FindWholeWordsOnly" flag to "false" to replace all text regardless of its surroundings.
options.findWholeWordsOnly = findWholeWordsOnly;

doc.range.replace("Jackson", "Louis", options);

expect(doc.getText().trim()).toEqual(
  findWholeWordsOnly ? "Louis will meet you in Jacksonville." : "Louis will meet you in Louisville." );

See Also