Is there a regex flavor that allows me to count the number of repetitions matched by the * and + operators?

You’re fortunate because in fact .NET regex does this (which I think is quite unique). Essentially in every Match, each Group stores every Captures that was made. So you can count how many times a repeatable pattern matched an input by: Making it a capturing group Counting how many captures were made by that group … Read more

Get index of each capture in a JavaScript regex

There is currently a proposal (stage 4) to implement this in native Javascript: RegExp Match Indices for ECMAScript ECMAScript RegExp Match Indices provide additional information about the start and end indices of captured substrings relative to the start of the input string. …We propose the adoption of an additional indices property on the array result … Read more

How to capture an arbitrary number of groups in JavaScript Regexp?

When you repeat a capturing group, in most flavors, only the last capture is kept; any previous capture is overwritten. In some flavor, e.g. .NET, you can get all intermediate captures, but this is not the case with Javascript. That is, in Javascript, if you have a pattern with N capturing groups, you can only … Read more

What is a non-capturing group in regular expressions?

Let me try to explain this with an example. Consider the following text: http://stackoverflow.com/ https://stackoverflow.com/questions/tagged/regex Now, if I apply the regex below over it… (https?|ftp)://([^/\r\n]+)(/[^\r\n]*)? … I would get the following result: Match “http://stackoverflow.com/” Group 1: “http” Group 2: “stackoverflow.com” Group 3: “https://stackoverflow.com/” Match “https://stackoverflow.com/questions/tagged/regex” Group 1: “https” Group 2: “stackoverflow.com” Group 3: “https://stackoverflow.com/questions/tagged/regex” But … Read more