JavaScript replace method takes functions as arguments

Hi,
I am pretty new to javascript.
I encountered some code in “Javascript the good part” from Douglas Crockford.

There is a way to passing callback function as second argument to string.replace() method.


var entity = {
    quot: '"',
    lt:   '<',
    gt:   '>'
};
var finalStr = '<&quot;>'.replace(/&([^&;]+);/g,
                      function (a, b) {
                          var r = entity[b];
                          return typeof r === 'string' ? r : a;
                      }
                   );


My questions are:

  1. How do I know what the number of parameters should be in callback function ( function(a,b) ) ?
  2. How do I know what will be passed in as a and b when I am defining the callback function?

Thanks in advance
David

If something still isn’t clear, let me know.

Thanks a lot