Skip to main content Link Search Menu Expand Document (external link)

replace() : Replace Text


On this page

Summary

Replace all occurrences of a substring with another string.

Signature

replace(str, find, replacement)

Parameters

Parameter Type Required Description
str String Yes The source string
find String Yes The substring to find
replacement String Yes The replacement text

Returns

Type: String

A new string with all occurrences replaced.


Examples

Simple Replacement


<p>{{replace(model.text, 'old', 'new')}}</p>

Data:

doc.Params["model"] = new {
    text = "The old car was sold to an old friend."
};

Output:

<p>The new car was snew to an new friend.</p>

Remove Characters

<p>Phone: </p>

Data:

doc.Params["model"] = new {
    phone = "555-123-4567"
};

Output:

<p>Phone: 5551234567</p>

Format Display

<p></p>

Data:

doc.Params["model"] = new {
    code = "PREMIUM_CUSTOMER_DISCOUNT"
};

Output:

<p>PREMIUM CUSTOMER DISCOUNT</p>

Sanitize Input


  <p></p>

Template Variable Replacement


<p>{{replace(replace(model.template, '{name}', model.name), '{date}', model.date)}}</p>

Data:

doc.Params["model"] = new {
    template = "Hello {name}, today is {date}",
    name = "John",
    date = "March 15"
};

Output:

<p>Hello John, today is March 15</p>

Notes

  • Replaces all occurrences (not just first)
  • Case-sensitive matching
  • Returns original string if find text not found
  • To remove text, use empty string as replacement
  • For pattern-based replacement, use regexSwap()
  • For single character replacement, more efficient than regex

See Also