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 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 either be set to ONE or ALL. Setting the scope to ONE replaces only the first occurrence of the substring. Setting the scope to ALL (the default) 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.", ...

Get Programming ColdFusion 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.