User Guide

Table Of Contents
152 Chapter 7: Using Regular Expressions in Functions
Note: To use backreferences in either the search string or the replace string, you must use
parentheses within the regular expression to create the corresponding subexpression. Otherwise,
ColdFusion throws an exception.
Using backreferences to perform case conversions in replacement strings
The
REReplace and REReplaceNoCase functions support special characters in replacement
strings to convert replacement characters to uppercase or lowercase. The following table describes
these special characters:
To include a literal \u, or other code, in a replacement string, escape it with another backslash; for
example \\u .
For example, the following statement replaces the uppercase string "HELLO" with a lowercase
"hello". This example uses backreferences to perform the replacement. For more information on
using backreferences, see “Using backreferences in replacement strings” on page 151.
reReplace("HELLO", "([[:upper:]]*)", "Don't shout\scream \L\1")
The result of this example is the string "Don't shout\scream hello".
Escaping special characters in replacement strings
You use the backslash character, \, to escape backreference and case-conversion characters in
replacement strings. For example, to include a literal "\u" in a replacement string, escape it, as in
"\\u".
Omitting subexpressions from backreferences
By default, a set of parentheses will both group the subexpression and capture its matched text for
later referral by backreferences. However, if you insert "?:" as the first characters of the
subexpression, ColdFusion performs all operations on the subexpression except that it will not
capture the corresponding text for use with a back reference.
This is useful when alternating over subexpressions containing differing numbers of groups would
complicate backreference numbering. For example, consider an expression to insert a "Mr." in
between Bonjour|Hi|Hello and Bond, using a nested group for alternating between Hi & Hello:
<cfset regex = "(Bonjour|H(?:i|ello))( Bond)">
<cfset replaceString = "\1 Mr.\2">
<cfset string = "Hello Bond">
#reReplace(string, regex, replaceString)#
Special
character
Description
\u Converts the next character to uppercase.
\l Converts the next character to lowercase.
\U Converts all characters to uppercase until encountering \E.
\L Converts all characters to lowercase until encountering \E.
\E End \U or \L.