Skip to main content

Replace

Replaces all occurrences of the specified text within this range.

Syntax​

expression.Replace(findWhat, replaceWith, matchCase, wholeWords);

expression - A variable that represents an ApiTextRange class.

Parameters​

NameRequired/OptionalData typeDefaultDescription
findWhatRequiredstringText to search for.
replaceWithRequiredstringReplacement text.
matchCaseOptionalbooleanfalseCase-sensitive search.
wholeWordsOptionalbooleanfalseMatch whole words only.

Returns​

ApiTextRange

Example​

Replace all occurrences of a word within a text range.

// Useful for bulk text substitution inside a shape.

// Replace every occurrence of "foo" with "bar".

const presentation = Api.GetPresentation();
const slide = presentation.GetSlideByIndex(0);
slide.RemoveAllObjects();

const fill = Api.CreateSolidFill(Api.RGB(255, 111, 61));
const stroke = Api.CreateStroke(0, Api.CreateNoFill());
const shape = Api.CreateShape("rect", 300 * 36000, 130 * 36000, fill, stroke);
shape.SetPosition(608400, 1267200);

const docContent = shape.GetDocContent();
docContent.GetElement(0).AddText("foo bar foo");
slide.AddObject(shape);

const range = shape.GetTextRange();
range.Replace("foo", "bar");