REReplace

Description

Uses a regular expression (RE) to search a string for a string pattern and replace it with another. The search is case sensitive.

Returns

If the scope parameter is set to one, returns a string with the first occurrence of the regular expression replaced by the value of substring. If the scope parameter is set to all, returns a string with all occurrences of the regular expression replaced by the value of substring. If the function finds no matches, it returns a copy of the string unchanged.

Syntax

REReplace(string,regex,substring,[scope])

Category

See also

History

ColdFusion (2018 release) Update 5: Added the flag useJavaAsRegexEngine to Application.cfc. Enable this flag to use Java Regex as the default regex engine. For more information, see Application variables. For information on using Regular Expression using the flag, see Using Regular Expressions.

ColdFusion MX: Added supports for the following special codes in a replacement substring, to control case conversion:

  • \u - uppercase the next character
  • \l - lowercase the next character
  • \U - uppercase until \E
  • \L - lowercase until \E
  • \E - end \U or \L
    For more information on new features, see REFind.

Parameters

Parameter

Description

string

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

regex

Regular expression to replace. The search is case sensitive.

substring

A string or a variable that contains one. Replaces reg_expression.

Also takes a callback function as an argument.

scope

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

Usage

For details on using regular expressions, see Using Regular Expressions in Functions in the Developing ColdFusion Applications.

Example 1

<cfscript>
    writeOutput(REReplace("CABARET","C|B","G","ALL") & "<br/>")
    writeOutput(REReplace("CABARET","C|B","G","ALL") & "<br/>")
    writeOutput(REReplace("CABARET","[A-Z]","G","ALL") & "<br/>")
    writeOutput(REReplace("CABARET","[A-Z]","G","ALL") & "<br/>")
    writeOutput(REReplace("I love jellies","jell(y|ies)","cookies") & "<br/>")
    writeOutput(REReplace("I love jellies","jell(y|ies)","cookies") & "<br/>")
    writeOutput(REReplace("I love jelly","jell(y|ies)","cookies") & "<br/>")
    writeOutput(REReplace("I love jelly","jell(y|ies)","cookies") & "<br/>")
</cfscript>

EXAMPLE 2

Try in CFFiddle

<cfscript>
    myStr = "PROSPER";
    outStr = reReplace( myStr, "P|S", function (transform, position, original, Count){ 
        writeOutput(Count)
        return "G"; 
    }, "all");
     writeoutput(outStr); 
</cfscript>

Output

123GROGGER

EXAMPLE 3

Try in CFFiddle

<cfscript>
    myStr = "PROSPER";
    outStr = reReplace( myStr, "P|S", function (transform, position, original, Count){ 
        writeOutput(Count)
        return "G"; 
    }, "one");
     writeoutput(outStr); 
</cfscript>

 Adobe

更快、更輕鬆地獲得協助

新的使用者?