• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

18. Regular Expressions in ColdFusion > 18.4. Backreferences

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, the regular expression is looking for any word followed by a space followed by the word from the first subexpression. The third parameter is the substring with which to replace the match made by the regular expression. Using \1 as the substring tells ColdFusion to replace any doubled words with the corresponding subexpression. Using All as the fourth parameter tells ColdFusion to apply the search and replace to all repeated words in the string.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint