<cffunction name="foo">
<cfargument name="default">
<cfoutput>#ARGUMENTS.default#</cfoutput>
<cfreturn ARGUMENTS.default />
</cffunction>
<cfset LOCAL.derp = "((bar))" />
<cfset LOCAL.derp = LOCAL.derp.replaceAll("\(\((.*)\)\)", foo('$1')) />
<cfoutput>#LOCAL.derp#</cfoutput>
The output I am expecting is bar bar however I am getting $1 bar. How can this be done in ColdFusion9?
The foo function is evaluated first and the string result of that function is what is subsequently passed to replaceAll, which has no idea its input was provided via a function.
String.replaceAll is a Java method which accepts a regex pattern string and a replacement string – you can’t pass a callback function in directly.
A solution to this is to use the cfRegex library I have created – this has a Replace function which lets you pass in a function to be executed against every match.
This could be used something like this: