Yes, I suspected that the real problem lies in my misunderstanding. I imagined that there is a mythical "replace" string that substitutes for the matched string. The "replace" of a regex replace is the entire resulting string, and not some discrete "diff" element. Thanks for helping me understand that better.
Perhaps what I am really looking to do (although it may be ill-conceived) is:
- Capture the array of matched substrings based on the first modifier argument
- loop through the array, performing a regex substitution on each substring based on the second argument.
- return the list of modified substrings
This algorithm is certainly not a standard "regex substitution". I'll have to think more about this to determine if it even makes sense to do. But at least you have helped me define the problem. Thanks!