Javascript: Passing a function with matches to replace( regex, func(arg) ) doesn't work Javascript: Passing a function with matches to replace( regex, func(arg) ) doesn't work google-chrome google-chrome

Javascript: Passing a function with matches to replace( regex, func(arg) ) doesn't work


$1 must be inside the string:

"string".replace(/st(ring)/, "gold $1") // output -> "gold ring"

with a function:

"string".replace(/st(ring)/, function (match, capture) {     return "gold " + capture + "|" + match;}); // output -> "gold ring|string"