JavaScript正则表达式重复(子)组 [英] JavaScript regexp repeating (sub)group

查看:88
本文介绍了JavaScript正则表达式重复(子)组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用正则表达式从单个调用中返回所有重复和匹配的子组?

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}}

其中属性的数量(即 id class attr1 )未定义并且可以是任何 key = value 对.

Where the number of attributes (i.e. id, class, attr1) are undefined and could be any key=value pair.

例如,目前我有以下 regexp和输出

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"]

似乎只匹配最后一组;有没有办法获取所有其他属性"( arg id )?

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.

推荐答案

这不可能在一个正则表达式中完成.JavaScript正则表达式只会将最后匹配的组返回给您,这正是您的问题.我前一段时间似乎遇到了这个问题:仅捕获Regex匹配中捕获组的最后一个实例.您可以在.Net中使用它,但这可能不是您所需要的.

This is impossible to do in one regular expression. JavaScript Regex will only return to you the last matched group which is exactly your problem. I had this seem issue a while back: Regex only capturing last instance of capture group in match. You can get this to work in .Net, but that's probably not what you need.

我确定您可以在正则表达式中弄清楚该怎么做,然后将第二组中的参数吐出来.

I'm sure you can figure out how to do this in a regular expressions, and the spit the arguments from the second group.

\{\{(\w+)\s+(.*?)\}\}

下面是一些JavaScript代码,向您展示它的完成方式:

Here's some javaScript code to show you how it's done:

var input = $('#input').text();
var regex = /\{\{(\w+)\s*(.*?)\}\}/g;
var match;
var attribs;
var kvp;
var output = '';

while ((match = regex.exec(input)) != null) {
    output += match[1] += ': <br/>';

    if (match.length > 2) {
        attribs = match[2].split(/\s+/g);
        for (var i = 0; i < attribs.length; i++) {
            kvp = attribs[i].split(/\s*=\s*/);
            output += ' - ' + kvp[0] + ' = ' + kvp[1] + '<br/>';       
        }
    }
}
$('#output').html(output);

jsFiddle

一个疯狂的想法是使用正则表达式并替换以将您的代码转换为json,然后使用JSON.parse进行解码.我知道以下是该想法的开始.

jsFiddle

A crazy idea would be to use a regex and replace to convert your code into json and then decode with JSON.parse. I know the following is a start to that idea.

/[\s\S]*?(?:\{\{(\w+)\s+(.*?)\}\}|$)/g.replace(input, doReplace);

function doReplace ($1, $2, $3) {
  if ($2) {
    return "'" + $2 + "': {" + 
      $3.replace(/\s+/g, ',')
        .replace(/=/g, ':')
        .replace(/(\w+)(?=:)/g, "'$1'") + '};\n';       
    }
   return '';
 }

这篇关于JavaScript正则表达式重复(子)组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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