简体   繁体   English

确定javascript正则表达式中有多少个捕获组?

[英]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)/ 非捕获组,例如/(?: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/ 您可以在这里查看它的运行情况: 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] . \\[(?:\\\\.|[^\\\\\\]])*\\] -匹配字符类,例如[abc]
  • \\\\. — Matches escaped characters. —匹配转义字符。
  • (\\()(?!\\?) — Matches opening parenthesis, that are not non-capturing nor look-ahead. (\\()(?!\\?) —匹配不但不捕捉也不超前的括号。
  • Any other regex construct can be safely skipped. 任何其他正则表达式构造都可以安全地跳过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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