User Guide Cancel

Replace

 

Description

Replaces occurrences of a string in a given string with either another string or the result of a callback function. Replaces the first occurrence by default, or can replace all. The search is case-sensitive. See ReplaceNoCase to perform case-insensitive searching.

Returns

The string, after making replacements.

Category

String functions

Function syntax

Replace(string, searchstring, replacestring|obj [, scope ][,start])
Replace(string, searchstring, replacestring|obj [, scope ][,start])
Replace(string, searchstring, replacestring|obj [, scope ][,start])

History

ColdFusion (2021 release):

  • Added the parameter start.
  • Also, callback support is added.

Parameters

Parameter

Description

string

A string or a variable that contains one. String in which to search.

searchstring

A string or a variable that contains one. String to search.

obj

Replacement string (or a variable that contains one) or a callback function that returns the replacement string. You can pass the callback function in this argument.

function(transform,position,original) {...}

scope

  • one: replaces the first occurrence (default)
  • all: replaces all occurrences

start

Position to start searching in the string (defaults to 1).

Usage

To remove a string, specify the empty string ("") as substring2. You do not need to escape comma characters in strings. For example, the following code deletes the commas from the sentence:

replace("The quick brown fox jumped over the lazy cow, dog, and cat.",",","","All")

Example

<cfoutput>#replace("The quick brown fox","o","cf","all")#</cfoutput>
<cfoutput>#replace("The quick brown fox","o","cf","all")#</cfoutput>
<cfoutput>#replace("The quick brown fox","o","cf","all")#</cfoutput>

The code produces the following output:

The quick brcfwn fcfx

Example 1

Example with callback function

<cfscript>
myStr="The quick brown fox jumped over the brown dog";
outStr = replace( myStr, "brown", function (transform, position, original){
return UCase(transform);
}, "all");
writeoutput(outStr);
</cfscript>
<cfscript> myStr="The quick brown fox jumped over the brown dog"; outStr = replace( myStr, "brown", function (transform, position, original){ return UCase(transform); }, "all"); writeoutput(outStr); </cfscript>
<cfscript>
    myStr="The quick brown fox jumped over the brown dog";
    outStr = replace( myStr, "brown", function (transform, position, original){
        return UCase(transform);
    }, "all");
    writeoutput(outStr);
</cfscript>

Output

The quick BROWN fox jumped over the BROWN dog

Example 2

<cfscript>
// Define the callback function
callback=(regexp,position,original)=>{
retString = regExp.reverse()&"aze"
return retString
}
baseStr="The quick brown fox jumped over the lazy cow."
writeOutput(replace(baseStr, "ow", callback, "all", len("The quick bro")) & "<br>")
writeOutput(replace(baseStr, "ow", callback, "all", len("The quick brown")) & "<br>")
</cfscript>
<cfscript> // Define the callback function callback=(regexp,position,original)=>{ retString = regExp.reverse()&"aze" return retString } baseStr="The quick brown fox jumped over the lazy cow." writeOutput(replace(baseStr, "ow", callback, "all", len("The quick bro")) & "<br>") writeOutput(replace(baseStr, "ow", callback, "all", len("The quick brown")) & "<br>") </cfscript>
<cfscript> 
  // Define the callback function 
  callback=(regexp,position,original)=>{ 
    retString = regExp.reverse()&"aze" 
    return retString 
  } 
  baseStr="The quick brown fox jumped over the lazy cow." 
  writeOutput(replace(baseStr, "ow", callback, "all", len("The quick bro"))  & "<br>") 
  writeOutput(replace(baseStr, "ow", callback, "all", len("The quick brown"))  & "<br>") 
</cfscript>

Output

The quick brwoazen fox jumped over the lazy cwoaze.
The quick brown fox jumped over the lazy cwoaze.

Note:

Usage

To remove a string, specify the empty string ("") as substring2. You do not need to escape comma characters in strings. For example, the following code deletes the commas from the sentence:

replace("The quick brown fox jumped over the lazy cow, dog, and cat.",",","","All")

Example

<cfoutput>
#replace("The quick brown fox","fox","dog")#
</cfoutput>
<cfoutput> #replace("The quick brown fox","fox","dog")# </cfoutput>
<cfoutput>
    #replace("The quick brown fox","fox","dog")#
</cfoutput>

The code produces the following output:

The quick brown dog

Example- usage of "all"

<cfoutput>
#replace("The quick brown fox jumped over the brown dog","brown","black")#
</cfoutput>
<cfoutput> #replace("The quick brown fox jumped over the brown dog","brown","black")# </cfoutput>
<cfoutput>
    #replace("The quick brown fox jumped over the brown dog","brown","black")#
</cfoutput>

Output

The quick black fox jumped over the brown dog

You can't use this replace to change "the" to "a" because replace is case-sensitive.

Get help faster and easier

New user?