ColdFusion – find single value replace with different values

For most of my life as a programmer, I was using the replace function in various contexts. I was searching for something and replacing the searched text with something else. Sometimes I was searching for multiple values and replacing them with multiple values – using the array of searched items and the array of replacements to put in place.

What if you want to search for something, but on each occurrence replace it with the different value? Till today I was not aware that this is possible in ColdFusion.

Why did I want to do such a thing? My code is generating a dynamic table of contents of the long document imported from XML, and I had to create unique anchors on each section of the document, along with the matching links in the table of contents.

Basic example

As I found in the ColdFusion replace function documentation, instead of the string to replace to, you can provide the callback function that will handle the replacement. Let’s take a look at this basic example:

<cfscript>
    textToReplace = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam faucibus tortor id consequat ultrices. Praesent et purus sapien. Aliquam tempor enim ac elementum aliquam. Duis vel enim maximus, luctus velit non, convallis est. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam vel magna non risus semper vehicula. Vestibulum eu fermentum est. Mauris sodales lorem mauris, sed commodo ligula convallis quis. Donec eu laoreet tellus. Maecenas in nisl id erat scelerisque condimentum eu at leo. Vivamus sagittis efficitur purus, sed elementum ipsum aliquet in.";

    anchorPositions = "";

    replacedText = replace(textToReplace, ".", function(transform, position, original) {
        anchorPositions = ListAppend(anchorPositions, position);
        return ". (anchor position: #position#)<br /> ";
    }, "ALL");
</cfscript>

<cfoutput>
    <div>
        <strong>Anchor positions: #anchorPositions#</strong>
    </div>
    <div>
        #replacedText#
    </div>
</cfoutput>

As you can see, on each occurrence of the dot (“.”) we are calling the function which is placing the information about the position of the dot. The function is also collecting the list of positions – it can be used later. This way we can generate proper anchors – each one with the unique ID based on the position.

The returned string will replace the current occurrence of the searched string. The searched string is provided to the function in the transform variable. Position in the original text is provided in the position variable and the original variable contains the whole contents of the original text to replace.

Implementation notes

The issue I found with this is that this is working in ColdFusion 2016 and newer. It is not working in ColdFusion 10. I was not able to test in ColdFusion 11.

One of the strengths of this technique is that you can use external variables in the replacement function. You can even use database queries – but make sure that you will not affect performance.