index.js
function replaceCaptureGroups (pattern, fn) {
  var start;
  var depth = 0;
  var escaped = false;

  for (var i = 0; i < pattern.length; i++) {
    var cur = pattern[i];
    if (escaped) {
      // skip this letter, it's been escaped
      escaped = false;
      continue;
    }
    switch (cur) {
      case '(':
        // we're only interested in groups when the depth reaches 0
        if (0 === depth) {
          start = i;
        }
        depth++;
        break;
      case ')':
        if (depth > 0) {
          depth--;

          // we're only interested in groups when the depth reaches 0
          if (0 === depth) {
            var end = i + 1;
            var l = start === 0 ? '' : pattern.substring(0, start);
            var r = pattern.substring(end);
            var v = String(fn(pattern.substring(start, end)));
            pattern = l + v + r;
            i = start;
          }
        }
        break;
      case '\\':
        escaped = true;
        break;
    }
  }
  return pattern;
}
function replaceCaptureGroups (pattern, fn) {
  var start;
  var depth = 0;
  var escaped = false;

  for (var i = 0; i < pattern.length; i++) {
    var cur = pattern[i];
    if (escaped) {
      // skip this letter, it's been escaped
      escaped = false;
      continue;
    }
    switch (cur) {
      case '(':
        // we're only interested in groups when the depth reaches 0
        if (0 === depth) {
          start = i;
        }
        depth++;
        break;
      case ')':
        if (depth > 0) {
          depth--;

          // we're only interested in groups when the depth reaches 0
          if (0 === depth) {
            var end = i + 1;
            var l = start === 0 ? '' : pattern.substring(0, start);
            var r = pattern.substring(end);
            var v = String(fn(pattern.substring(start, end)));
            pattern = l + v + r;
            i = start;
          }
        }
        break;
      case '\\':
        escaped = true;
        break;
    }
  }
  return pattern;
}
function replaceCaptureGroups ( pattern , fn ) { var start ; var depth = 0 ; var escaped = false ; for ( var i = 0 ; i < pattern . length ; i ++ ) { var cur = pattern [ i ] ; if ( escaped ) { // skip this letter, it's been escaped escaped = false ; continue ; } switch ( cur ) { case '(' : // we're only interested in groups when the depth reaches 0 if ( 0 === depth ) { start = i ; } depth ++ ; break ; case ')' : if ( depth > 0 ) { depth -- ; // we're only interested in groups when the depth reaches 0 if ( 0 === depth ) { var end = i + 1 ; var l = start === 0 ? '' : pattern . substring ( 0 , start ) ; var r = pattern . substring ( end ) ; var v = String ( fn ( pattern . substring ( start , end ) ) ) ; pattern = l + v + r ; i = start ; } } break ; case '\\' : escaped = true ; break ; } } return pattern ; }
Invokes `fn` for each "capture group" encountered in the PCRE `pattern`,
and inserts the returned value into the pattern instead of the capture
group itself.

@private
Invokes fn for each capture group encountered in the PCRE pattern and inserts the returned value into the pattern instead of the capture group itself .