Can a regular expression capture all repeating and matching subgroups in one call?
Consider a string like this:
{{token id=foo1 class=foo2 attr1=foo3}}
Where the number of attributes (e.g. id
, class
, attr1
) are variable and can be any key=value
pair.
Currently, the regex and output are as follows here
var pattern = /\{{([\w\.]+)(?:\s+(\w+)=(?:("(?:[^"]*)")|([\w\.]+)))*\}\}/;
var str = '{{token arg=1 id=2 class=3}}';
var matches = str.match(pattern);
// -> ["{{token arg=1 id=2 class=3}}", "token", "class", undefined, "3"]
It appears to only match the last group; Is there a way to retrieve all the other "attributes" (arg
and id
)?
Please note: this example demonstrates matching in a single string, but the pattern may appear in a much larger string with multiple matches. Therefore, ^
and $
cannot be used.