When you repeat a capture group, in most tastes only the last capture is saved; any previous capture is overwritten. In some flavor, like .NET, you can get all intermediate captures, but this is not the case with Javascript.
That is, in Javascript, if you have a template with N capture groups, you can capture exactly N lines per match, even if some of these groups were repeated.
So, in general, depending on what you need to do:
- If this is an option, divide by separator instead
- Instead of matching
/(pattern)+/ , possibly match /pattern/g , perhaps in an exec loop- Note that these two are not completely equivalent, but this may be an option.
- Multilevel Compliance:
- Capture a repeating group in one match
- Then run another regex to break this match
Recommendations
example
Here's an example of matching <some;words;here> in a text using an exec loop and then splitting ; get single words ( see also at ideone.com ):
var text = "a;b;<c;d;e;f>;g;h;i;<no no no>;j;k;<xx;yy;zz>"; var r = /<(\w+(;\w+)*)>/g; var match; while ((match = r.exec(text)) != null) { print(match[1].split(";")); }
Used pattern:
_2__ / \ <(\w+(;\w+)*)> \__________/ 1
This corresponds to <word> , <word;another> , <word;another;please> , etc. Group 2 is repeated to capture any number of words, but it can only hold the last capture. The entire list of words is captured by group 1; this line is then split into a comma separator.
Related Questions
- How to access mapped groups in javascript regex?
polygenelubricants Aug 21 2018-10-21 14:24
source share