Replaces occurrences of substring1 in a string with substring2, in a specified scope. The search is case-sensitive. Returns a string with replacements made.

replace(String, substring1, substring2 [, scope]) → returns String

replace Argument Reference

String String

String to search

substring1 String

Substring to find within string

substring2 String

Substring to replace substring1 with. As of CF 2016+ you can also pass a callback function in this argument function(pattern, position, originalString).

scope String
Default: one

* one: replace the first occurrence
* all: replace all occurrences
  • one
  • all

Examples sample code invoking the replace function

Simple Replace Example

replace("ColdFUsion", "U", "u")

Expected Result: ColdFusion

Example replaing one instance

replace("COldFusiOn", "O", "o", "ONE")

Expected Result: ColdFusiOn

Example using Callback Function

In CF 2016+ you can pass in a callback function to the third argument of the replace function

public function upCase(pattern, position, orig) {
    return uCase(pattern);
result = replace("A man a plan a canal.","an", upCase , "ALL");

Expected Result: A mAN a plAN a cANal.

Fork me on GitHub