I have a function:
function process(str) {
if (str == "Mary") {
return "Sally";
} else {
return str;
}
}
And I have some text:
John Smith
Mary Smith
Peter Smith
Mary Davis
I want it become:
John Smith
Sally Smith
Peter Smith
Sally Davis
But none of this work:
text = text.replace(/([a-zA-Z]+)\s([a-zA-Z]+)([\n]{0,1})/g,process(RegExp.$1) + " " + process(RegExp.$2)+"\$3");
//RegExp.$n is undefined
or
text = text.replace(/([a-zA-Z]+)\s([a-zA-Z]+)([\n]{0,1})/g,process('\$1') + " " + process('\$2')+"\$3");
//I got this exact string '$1' in process()
So how can I reuse the matches value and pass it into another function?