Is it possible to return all the repeating and matching subgroups from a single call with a regular expression?
For example, I have a string like :
{{token id=foo1 class=foo2 attr1=foo3}}
Where the number of attributes (i.e. id
, class
, attr1
) are undefined and could be any key=value
pair.
For example, at the moement, I have the following regexp and output
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 seems that it only matches the last group; Is there any way to get all the other "attributes" (arg
and id
)?
Note: the example illustrate match on a single string, but the searched pattern be be located in a much larger string, possibly containing many matches. So, ^
and $
cannot be used.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…