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

I would expect this JavaScript line:

"foo bar baz".match(/^(\s*\w+)+$/) 

to return something like:

 ["foo bar baz", "foo", " bar", " baz"] 

but instead, it returns only the last recorded match:

 ["foo bar baz", " baz"] 

Is there a way to get all captured matches?

+67
javascript regex capturing-group
Aug 21 2018-10-21T00:
source share
4 answers

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(";")); } // c,d,e,f // xx,yy,zz 

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?
+76
Aug 21 2018-10-21
source share

How about this? "foo bar baz".match(/(\w+)+/g)

+7
Aug 21 '10 at 14:10
source share

Unless you have a more complex requirement on how you split your lines, you can split them and then return them to the original line:

 var data = "foo bar baz"; var pieces = data.split(' '); pieces.unshift(data); 
+5
Aug 21 '10 at 14:22
source share

try using 'g':

 "foo bar baz".match(/\w+/g) 
+3
Aug 21 '10 at 15:41
source share



All Articles