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)/
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.
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.