Given an input string, I need to find and replace this:
funccall(x, y, z, w)
With this:
call(x, y, 0, z, w)
Everything between the commas, as well between a comma and a bracket, is unknown (i.e., may be different on each input). This includes the values of x, y, z and w, as well as the amount of spaces in between.
But the funccall
part is constant (i.e., that's the actual substring that I should be looking for), and the fact that I need to insert 0,
after the 2nd comma is also constant.
Here is my method:
function fix(str) {
const index0 = str.indexOf('funccall');
if (index0 >= 0) {
const index1 = str.indexOf(',', str.indexOf(',', index0) + 1);
const part0 = str.substring(0, index0);
const part1 = str.substring(index0 + 'func'.length, index1) + ', 0';
const part2 = str.substring(index1);
return part0 + part1 + part2;
}
return str;
}
I was really hoping for a one-liner solution (or at least something close to a one-liner).