如何在 JavaScript Regexp 中捕获任意数量的组? [英] How to capture an arbitrary number of groups in JavaScript Regexp?

查看:23
本文介绍了如何在 JavaScript Regexp 中捕获任意数量的组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这行 JavaScript:

I would expect this line of JavaScript:

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

返回如下内容:

["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?

推荐答案

当你重复一个捕获组时,在大多数风格中,只保留最后一个捕获;任何先前的捕获都会被覆盖.在某些风味中,例如.NET,您可以获得所有中间捕获,但 Javascript 不是这种情况.

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.

也就是说,在 Javascript 中,如果您有一个包含 N 个捕获组的模式,则每个匹配项只能准确捕获 N 个字符串,即使其中一些组是重复.

That is, in Javascript, if you have a pattern with N capturing groups, you can only capture exactly N strings per match, even if some of those groups were repeated.

所以一般来说,取决于你需要做什么:

So generally speaking, depending on what you need to do:

  • 如果它是一个选项,请改为使用分隔符拆分
  • 可能匹配 /(pattern)+/ 而不是匹配 /pattern/g,也许在 exec 循环中
    • 请注意,这两者并不完全相同,但可能是一种选择
    • If it's an option, split on delimiters instead
    • Instead of matching /(pattern)+/, maybe match /pattern/g, perhaps in an exec loop
      • Do note that these two aren't exactly equivalent, but it may be an option
      • 在一场比赛中捕获重复的组
      • 然后运行另一个正则表达式来分解匹配
      • regular-expressions.info/重复捕获组与捕获重复组

        这是一个在文本中匹配 的示例,使用 exec 循环,然后在 ; 获取单个单词(另见 ideone.com):

        Here's an example of matching <some;words;here> in a text, using an exec loop, and then splitting on ; to get individual words (see also on 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
        

        使用的模式是:

              _2__
             /    
        <(w+(;w+)*)>
         \__________/
              1
        

        这匹配等.2 重复捕获任意数量的单词,但只能保留最后一次捕获.整个单词列表由第 1 组捕获;这个字符串然后在分号分隔符上split.

        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 split on the semicolon delimiter.

        这篇关于如何在 JavaScript Regexp 中捕获任意数量的组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆