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);
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson