title: Javascript: Passing a function with matches to replace( regex, func(arg) ) doesn't work
body: <p>According to this site the following replace method should work, though I am sceptical.
  <a href="http://www.bennadel.com/blog/55-Using-Methods-in-Javascript-Replace-Method.htm" rel="noreferrer">http://www.bennadel.com/blog/55-Using-Methods-in-Javascript-Replace-Method.htm</a></p>
  
  <p>My code is as follows:</p>
  
  <pre><code>text = text.replace( 
      new Regex(...),  
      match($1) //$.. any match argument passed to the userfunction 'match',
                //    which itself invokes a userfunction
  );
  </code></pre>
  
  <p>I am using Chrome 14, and do not get passed any parameters passed to the function match?</p>
  
  <p><strong>Update</strong>:</p>
  
  <p>It works when using</p>
  
  <pre><code>text.replace( /.../g, myfunc($1) );
  </code></pre>
  
  <p>The JavaScript interpreter expects a <em>closure</em>, - apparent userfunctions seem to lead to scope issues i.e. further userfunctions will not be invoked. Initially I wanted to avoid closures to prevent necessary memory consumption, but there are already safeguards.</p>
  
  <p>To pass the arguments to your own function do it like this (wherein the argument[0] will contain the entire match:</p>
  
  <pre><code>result= text.replace(reg , function (){
          return wrapper(arguments[0]);
  });
  </code></pre>
  
  <p>Additionally I had a problem in the string-escaping and thus the RegEx expression, as follows:</p>
  
  <p><code>/\s......\s/g</code></p>
  
  <p>is not the same as </p>
  
  <p><code>new Regex ("\s......\s" , "g")</code> or<br>
  <code>new Regex ('\s......\s' , "g")</code></p>
  
  <p>so be careful!</p>