###How Can I Do a Replace for EACH RegEx Match Found, as it is Found?
###Use Case
- I have a string with a number of substrings that have the same pattern
- I need replace each occurrence (match) with an individual result, which requires a formula and/or a function (or multiple KM Actions to calculate the replacement)
- Examples:
- Replace all dates in one format with another format
- Increment a seq# at the end of a string
- For a real example, see this KM Topic: Replacing Substrings in Clipboard or Variable.
###JavaScript Solution
This is easily done in JavaScript
var sourceData = `
test1 on line 1
line that should not match 2
test25 on line 3
another line no match 4
test3 on line 5
another no match 6
`
var reTestNum = /^(test)(\d+)/gm;
//--- ADD 10 TO Number AFTER 'test' ---
var updatedData = sourceData.replace(reTestNum, function ($0, $1, $2) {
var num = parseInt($2, 10) + 10;
return $1 + num;
});
console.log(updatedData);
//--- RESULTS ---
/*
test11 on line 1
line that should not match 2
test35 on line 3
another line no match 4
test13 on line 5
another no match 6
*/
But I have been unable to design a KM solution that does not use a script.
Any ideas?
What I need is a way to update the source string for each match that is found in a KM For Each action (KM Wiki):
###This is What I'd Like to To Do
but is NOT currently supported.
Is there another way to accomplish this?