如何在 JavaScript Regexp 中捕获任意数量的组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3537878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to capture an arbitrary number of groups in JavaScript Regexp?
提问by disc0dancer
I would expect this line of JavaScript:
我希望这行 JavaScript:
"foo bar baz".match(/^(\s*\w+)+$/)
to return something like:
返回类似的东西:
["foo bar baz", "foo", " bar", " baz"]
but instead it returns only the last captured match:
但它只返回最后捕获的匹配项:
["foo bar baz", " baz"]
Is there a way to get all the captured matches?
有没有办法获得所有捕获的匹配项?
回答by polygenelubricants
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.
当您重复一个捕获组时,在大多数情况下,只保留最后一个捕获;任何先前的捕获都会被覆盖。在某些风格中,例如 .NET,您可以获得所有中间捕获,但 Javascript 则不是这种情况。
That is, in Javascript, if you have a pattern with Ncapturing groups, you can only capture exactly Nstrings per match, even if some of those groups were repeated.
也就是说,在 Javascript 中,如果您有一个包含N 个捕获组的模式,则每个匹配项只能捕获N 个字符串,即使其中一些组重复了。
So generally speaking, depending on what you need to do:
所以一般来说,取决于你需要做什么:
- If it's an option, split on delimiters instead
- Instead of matching
/(pattern)+/, maybe match/pattern/g, perhaps in anexecloop- Do note that these two aren't exactly equivalent, but it may be an option
- Do multilevel matching:
- Capture the repeated group in one match
- Then run another regex to break that match apart
- 如果它是一个选项,请在分隔符上拆分
- 而不是匹配
/(pattern)+/,也许是匹配/pattern/g,也许是在一个exec循环中- 请注意,这两个并不完全相同,但它可能是一种选择
- 做多级匹配:
- 在一场比赛中捕获重复的组
- 然后运行另一个正则表达式来打破这场比赛
References
参考
Example
例子
Here's an example of matching <some;words;here>in a text, using an execloop, and then splitting on ;to get individual words (see also on ideone.com):
这是<some;words;here>在文本中匹配的示例,使用exec循环,然后拆分;以获取单个单词(另请参见 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
The pattern used is:
使用的模式是:
_2__
/ \
<(\w+(;\w+)*)>
\__________/
1
This matches <word>, <word;another>, <word;another;please>, etc. Group 2 is repeated to capture any number of words, but it can only keep the last capture. The entire list of words is captured by group 1; this string is then spliton the semicolon delimiter.
这匹配<word>, <word;another>,<word;another;please>等。重复第 2 组以捕获任意数量的单词,但它只能保留最后一个捕获。整个单词列表由第 1 组捕获;然后这个字符串位于split分号分隔符上。
Related questions
相关问题
回答by meder omuraliev
How's about this? "foo bar baz".match(/(\w+)+/g)
这个怎么样? "foo bar baz".match(/(\w+)+/g)
回答by g.d.d.c
Unless you have a more complicated requirement for how you're splitting your strings, you can split them, and then return the initial string with them:
除非您对如何拆分字符串有更复杂的要求,否则您可以拆分它们,然后用它们返回初始字符串:
var data = "foo bar baz";
var pieces = data.split(' ');
pieces.unshift(data);
回答by Jet
try using 'g':
尝试使用“g”:
"foo bar baz".match(/\w+/g)

