This'll be a lot quicker than I anticipated, given Adobe haven't actually bothered to implement most of the given feature.
Back in the ColdFusion 10 era, I raised 3597432 ("
replaceWithCallback()
"). The idea here was to add an option to replace() to make it behave more like JavaScript's String.prototype.replace()
, in that it can take a callback to perform the replacement operation. Quite often a replacement operation might be mroe than a simple swap-out for one value for another, beyond the scope that regex operations can facilitate it. So using a callback allows one to handle the replacement anyway one likes.Here's an example of the JavaScript version:
source = "this is a string from which to exchange some matches using a callback to perform the exchange";
match = new RegExp("exchange", "g");
replacement = "replace";
result = source.replace(match, function(match, index, source){
return replacement;
});
console.log(result);
Which yields:
C:\src\CF12\strings\replaceWithCallback>node replace.js
this is a string from which to replace some matches using a callback to perform the replace
C:\src\CF12\strings\replaceWithCallback>
this is a string from which to replace some matches using a callback to perform the replace
C:\src\CF12\strings\replaceWithCallback>
Note how this just swaps out all the instances of "exchange" for "replace". This is a daft usage of a callback, but you get the general idea.
So ColdFusion 2016 implements this:
source = "this is a string from which to exchange some matches using a callback to perform the exchange";
match = "exchange";
replacement = "replace";
result = source.replace(match, function(match, index, source){
return replacement;
}, "ALL");
writeDump([
source=source,
match=match,
replacement=replacement,
result = result
]);
Result:
So that's cool. It works.
But do you know what's not cool?
They only implemented it on
replace()
. They did not implement it on replaceNoCase()
. Nor reReplace()
. nor reReplaceNoCase()
.Sigh. Useless. Just useless.
So really... in that they only added it to one of four congruent functions, this represents making CFML (haha, I initially typoed that as "FML"... somewhat fittingly perhaps) just that little bit worse, rather than that little bit better.
Righto.
--
Adam