CFLib.org – Common Function Library Project

REEscape(theString)

Last updated June 26, 2002

author

Shawn Seley

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Escapes all regular expression "special characters" in a string with "\". Useful for dynamic uses of REFind and REReplace.

Return Values:
Returns a string.

Example:

<cfoutput>
"this and (that)" => #REEscape("this and (that)")#<br>
"$1 + $2 = $3" => #REEscape("$1 + $2 = $3")#<br>
"\,+,*,?,.,[,],^,$,(,),{,},|,-" => #REEscape("\,+,*,?,.,[,],^,$,(,),{,},|,-")#<br>
<br>
<cfset price = "$3">
Escaped regular expression search string that works:<br>
"= #price#" found at character: #REFind("=.*#REEscape(price)#",  "An example of simple financial math: $1 + $2 = $3")#<br>
<br>
Unescaped regular expression search string that doesn't work:<br>
"= #price#" not found (search returns zero): #REFind("=.*#price#",  "An example of simple financial math: $1 + $2 = $3")#
</cfoutput>

Parameters:

Name Description Required
theString The string to format. Yes

Full UDF Source:

/**
 * Escapes all regular expression &quot;special characters&quot; in a string with &quot;\&quot;.
 * 
 * @param theString      The string to format. (Required)
 * @return Returns a string. 
 * @author Shawn Seley (shawnse@aol.com) 
 * @version 1, June 26, 2002 
 */
function REEscape(theString){
    var special_char_list      = "\,+,*,?,.,[,],^,$,(,),{,},|,-";
    var esc_special_char_list  = "\\,\+,\*,\?,\.,\[,\],\^,\$,\(,\),\{,\},\|,\-";
    return ReplaceList(theString, special_char_list, esc_special_char_list);
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson