简体   繁体   中英

Determine how many capturing groups are in a javascript regexp?

I'm working on a utility to merge multiple regular expressions into one. I want to support replace with a function, but that means I need to have an offset for the capturing groups so I can pass the correct arguments to the replacer function. Here's the simplest solution I have found:

function countCapturingGroups(regexp) {
  var count = 0;
  regexp.source.replace(/(\\*)\((\?:)?/g,
      function(full, backslashes, nonCapturing) {
    if (backslashes.length % 2 === 0 && !nonCapturing) {
      count++;
    }
  });
  return count;
}

This supports:

  1. Any number of backslashes (an even number means the backslash is itself escaped)
  2. Non-capturing groups, eg /(?:this)/

Am I overlooking any other valid ways to use parentheses that won't capture content?

You can see it in action here: http://jsfiddle.net/theazureshadow/RHdPP/

function countCapturingGroups(regex) {
    var count = 0;
    regex.source.replace(/\[(?:\\.|[^\\\]])*\]|\\.|(\()(?!\?)/g,
        function (full, capturing) {
            if (capturing) count++;
        });
    return count;
}
  • \\[(?:\\\\.|[^\\\\\\]])*\\] — Matches character classes, like [abc] .
  • \\\\. — Matches escaped characters.
  • (\\()(?!\\?) — Matches opening parenthesis, that are not non-capturing nor look-ahead.
  • Any other regex construct can be safely skipped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM