[英]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: 这支持:
/(?: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. (\\()(?!\\?)
—匹配不但不捕捉也不超前的括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.