Is there something like PHP's preg_replace_callback() in javascript?
Why, yes, you can do exactly that: str.replace(pattern, function () { ... })
.
Here's some documentation: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/replace
Yes
var s2 = s1.replace(/regex/, function(whole, part1, part2, ...) { ... })
The function is passed the whole matched string as the first argument. If there are any capturing groups, those are passed as subsequent arguments.