I would like to get a deeper understanding of the nuances of javascript.
Take the function below:
str.replace(/(\b[^:]+):'([^']+)'/g, function ($0, param, value) {
ret[param] = value;
});
- Why is there a need for /g?
- What
exactly gets passed into the
function? - Where do these values come
from?
Thanks!
Because presumably you will have multiple pairs on the matching string, e.g.
a:'b' c:'d'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:
The callback will be executed twice, and it will show you
"a:'b'"for$0,"a"forparamand"b"forvalueon the first execution.In the second execution -for the second match-, will show you
"c:'d'"for$0,"c"forparamand"d"for value.