REReplace() + Java + Function Pointers = Freakin' Sexy!

<cffunction name="JREReplace" access="public" returntype="string" output="false"
	hint="This performs Java REReplaces on a string.">
	 
	<!--- Define arguments. --->
	<cfargument name="Text" type="string" required="true" />
	<cfargument name="Pattern" type="string" required="true" />
	<cfargument name="Target" type="any" required="true" />
	<cfargument name="Scope" type="string" required="false" default="ONE" />
	 
	<cfscript>
		 
		// Define the local scope.
		var LOCAL = StructNew();
		 
		// Check to see if we are using a string replace or a method
		// helper replace.
		if (IsSimpleValue( ARGUMENTS.Target )){
			 
			// We are doing a standard string replace, so just
			// use Java's string replacement. Check the scope.
			if (NOT Compare( ARGUMENTS.Scope, "ALL" )){
				 
				// Replace all.
				return( 
					CreateObject( "java", "java.lang.String" ).Init( 
						ARGUMENTS.Text 
						).ReplaceAll(
							ARGUMENTS.Pattern, ARGUMENTS.Target 
							)
					);
			 
			} else {
				 
				// Replace one.
				return( 
					CreateObject( "java", "java.lang.String" ).Init( 
						ARGUMENTS.Text 
						).ReplaceFirst( 
							ARGUMENTS.Pattern, 
							ARGUMENTS.Target 
							)
					);
			 
			}
		 
		} else {
			 
			// We are using a function here to replace out the
			// groups. That means that matches have to be 
			// evaluated and replaced on an individual basis.
			// Create the java pattern complied to the given regular 
			// expression. 
			LOCAL.Pattern = CreateObject( 
				"java", 
				"java.util.regex.Pattern" 
				).Compile( 
					ARGUMENTS.Pattern
				);
			 
			// Create the java matcher based on the given text using the 
			// compiled regular expression.
			LOCAL.Matcher = LOCAL.Pattern.Matcher( ARGUMENTS.Text );
			 
			// Create a string buffer to hold the results.
			LOCAL.Results = CreateObject( 
				"java", 
				"java.lang.StringBuffer" 
				).Init();
			 
			// Loop over the matcher while we still have matches.
			while ( LOCAL.Matcher.Find() ){
				 
				// We are going to build an array of matches. 
				LOCAL.Groups = ArrayNew( 1 );
				for (
					LOCAL.GroupIndex = 1 ; 
					LOCAL.GroupIndex LTE LOCAL.Matcher.GroupCount() ; 
					LOCAL.GroupIndex = (LOCAL.GroupIndex + 1)
					){
					 
					// Add the current group to the array of groups.
					ArrayAppend( 
						LOCAL.Groups, 
						LOCAL.Matcher.Group( JavaCast( "int", LOCAL.GroupIndex ) ) 
						);
				 
				}
				 
				// Replace the current match. Be sure to get the value by
				// using the helper function.
				LOCAL.Matcher.AppendReplacement(
					LOCAL.Results,
					 
					// Call the target function pointer using function notation.
					ARGUMENTS.Target( 
						LOCAL.Matcher.Group(), 
						LOCAL.Groups 
						)
					);
				// Check to see if we need to break out of this.
				if (NOT Compare( ARGUMENTS.Scope, "ONE" )){
					break;
				}
			 
			}
			 
			// Add what ever is left of the text.
			LOCAL.Matcher.AppendTail( LOCAL.Results );
			 
			// Return the string buffer.
			return( LOCAL.Results.ToString() );
		}
		 
	</cfscript>
</cffunction>

For Cut-and-Paste