CAUTION: This is primarily a note to self.
jreReplaceEach( targetText, patternText, callback ) :: String
... where the Callback will receive the following invocation arguments on each pattern match:
- The full match.
- Each captured groups as spread arguments (2...N).
- The 1-based offset of the match.
- The full target text.
To see this in action, I'm going to take the content of a poem that uses lower-case characters and then upper-cases the first character of each line:
As you can see, we're using a multi-line Regular Expression pattern in order to target individual lines of text in the poem. Then, we're replacing the first character of each line with its upper-cased counterpart. And, when we run the above code, we get the following output:
Works like a charm! I was able to inspect each individual Regular Expression pattern match in my ColdFusion closure and then return a customized replacement string.
Working with Regular Expression patterns always make me happy, like a pig in slop. And now, hopefully when I need to remember how to use ColdFusion closures to leverage the Java Pattern Matching library, I'll be able to find it a bit more easily.