Backreferences

Backreferences allow your regular expressions to refer to previously matched subexpressions. This is useful in situations where you want your regular expression to use a match as part of the criteria for an additional regular-expression function. A commonly used scenario illustrating backreferencing involves using a regular expression to remove doubled words within a sentence. For example, suppose you have the string “I would like to go to to the park.” The word “to” appears twice in a row—a common typing mistake. The regular expression to remove any doubled words from the sentence looks like this:

<cfset NoDupes = REReplaceNoCase("I would like to go to to the park.", 
" \b([\w]+)[ ]+\1", "\1", "All")>

Let’s break this example down so that we can get a better understanding of what is going on. We use the REReplaceNoCase( ) function to perform the search and replace without regard to case. The first parameter of the function is the string we want to perform the search and replace on. The second parameter is the actual regular expression. The “\b([\w]+)” tells ColdFusion to look for any word. Notice that this part of the regular expression is enclosed in parentheses, making it a subexpression. The next part of the regular expression “[ ]+” tells ColdFusion to look for one or more spaces. The third part of the regular expression, \1, is the backreference and tells ColdFusion to refer back to the first subexpression and use it as part of the search criteria. In this case, ...

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.