Why is there a need for /g?
Because presumably you will have multiple pairs on the matching string, e.g. a:'b' c:'d'
What exactly gets passed into the function?
The callback function gets the whole match as the first argument, the two later arguments are the capturing groups specified on your regexp.
For example:
"a:'b' c:'d'".replace(/(\b[^:]+):'([^']+)'/g, function ($0, param, value) {
console.log($0, param, value);
});
The callback will be executed twice, and it will show you "a:'b'"
for $0
, "a"
for param
and "b"
for value
on the first execution.
In the second execution -for the second match-, will show you "c:'d'"
for $0
, "c"
for param
and "d"
for value.