Articles

jreEscape UDF

Escapes regular expression control characters within a string

Image about jreEscape UDF
April 19, 2025

The jreEscape user-defined function (UDF) ensures strings can be safely used in regex patterns by preventing special characters from being interpreted as regex metacharacters. The reescape function exists in Adobe ColdFusion, but not Lucee.

Source Code

<cfscript>
public string function jreEscape(required string text="") output=false hint="alternative to ACF-only reEscape()" {
	return tostring(arguments.text).replaceAll("([\\.+*?^$()\[\]{}|])", "\\$1");
}
</cfscript>

Demo

<cfscript>
tests = [
	"*.{}[]exam?ple"
	,"Hello. World*+?"
	,"Test^|[(])"
	,""
	,"NoSpecialChars"
	,".*+?^${}()|[]\"
	,"Multiple...Dots"
	,"Back\slash"
];

results = [:];

for(test in tests){
	results[test] = [
		"jreEscape": jreEscape(test)
		,"reEscape": reEscape(test)
	];
	results[test]["same"] = results[test].jreEscape eq results[test].reEscape;
}

writedump(var=results, label="jreEscape Results");
</cfscript>