Replacing Strings
String
replacement is performed with the REReplace( )
and
REReplaceNoCase( )
functions. REReplace(
)
performs case-sensitive search and replace functions,
while REReplaceNoCase( )
is used for
case-insensitive search and replace. Regardless of which tag you use,
they have the same
syntax:
REReplace(String, RegularExpression, Substring
[,Scope
]) REReplaceNoCase(String, RegularExpression, Substring
[,Scope
])
Both functions take three required parameters and one optional
parameter. The first required parameter is the string to perform the
regular expression on. The string can be either a block of text
enclosed in single or double quotes or a variable that contains the
text that you want to search. The second required parameter is the
regular expression you want to use for your search. Any valid
ColdFusion regular expression can be used. The third required
parameter is the substring. The substring contains the string to
replace any matches made by the regular expression. The final
parameter is optional and determines the scope of the
search-and-replace operation. The scope can be set to
One
or All
. Setting the scope
to One
(the default) replaces only the first
occurrence of the substring; setting it to All
replaces all occurrences of the substring. The following two examples
show the use of the REReplace( )
and
REReplaceNoCase( )
functions:
Replace all occurrences of the number 7 with the word “seven”:
REReplace("There are 7 words in this string.", "7", "Seven", ...
Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.