title: .NET's Match.Result in javascript
body: <p>Is there an equivalent to Match.Result from .NET in javascript so that using a replace function, one can still use the convenient substitutions for perhaps part of the logic?</p>
  
  <p>Or does one need to provide a custom yet simple function like the following which seems to work in all cases?</p>
  
  <pre><code>RegExp.matchResult = function (subexp, offset, str, matches) {
      return subexp.replace(/\$(\$|&amp;|`|\'|[0-9]+)/g, function (m, p) {
          if (p === '$') return '$';
          if (p === '`') return str.slice(0, offset);
          if (p === '\'') return str.slice(offset + matches[0].length);
          if (p === '&amp;' || parseInt(p, 10) &lt;= 0 || parseInt(p, 10) &gt;= matches.length) return matches[0];
          return matches[parseInt(p, 10)];
      });
  };
  var subexp; //fill in with substitution expression
  var replaceFunc = function () {
      return RegExp.matchResult(subexp, arguments[arguments.length - 2], arguments[arguments.length - 1], Array.prototype.slice.call(arguments).slice(0, -2));
  };
  </code></pre>