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

查看:178
本文介绍了如何在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 / 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/重复捕获组与捕获重复组

        • regular-expressions.info/Repeating a Capturing Group vs Capturing a Repeating Group
          • Javascript flavor notes

          以下是匹配< some; words; here>的示例在文本中,使用 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
          

          匹配 < word> < word;另一个> < word;另一个;请> 等。重复组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.

          • How do you access the matched groups in a javascript regex?

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

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