javascript 正则表达式抓取方括号之间的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7201400/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:16:38  来源:igfitidea点击:

Regex to grab strings between square brackets

javascriptregexmatch

提问by cabaret

I have the following string: pass[1][2011-08-21][total_passes]

我有以下字符串: pass[1][2011-08-21][total_passes]

How would I extract the items between the square brackets into an array? I tried

如何将方括号之间的项目提取到数组中?我试过

match(/\[(.*?)\]/);

match(/\[(.*?)\]/);

var s = 'pass[1][2011-08-21][total_passes]';
var result = s.match(/\[(.*?)\]/);

console.log(result);

but this only returns [1].

但这只会返回[1].

Not sure how to do this.. Thanks in advance.

不知道如何做到这一点.. 提前致谢。

回答by Kobi

You are almost there, you just need a global match(note the /gflag):

你快到了,你只需要一个全局匹配(注意/g标志):

match(/\[(.*?)\]/g);

Example: http://jsfiddle.net/kobi/Rbdj4/

示例:http: //jsfiddle.net/kobi/Rbdj4/

If you want something that only captures the group (from MDN):

如果您想要仅捕获组的内容(来自MDN):

var s = "pass[1][2011-08-21][total_passes]";
var matches = [];

var pattern = /\[(.*?)\]/g;
var match;
while ((match = pattern.exec(s)) != null)
{
  matches.push(match[1]);
}

Example: http://jsfiddle.net/kobi/6a7XN/

示例:http: //jsfiddle.net/kobi/6a7XN/

Another option (which I usually prefer), is abusing the replace callback:

另一种选择(我通常更喜欢)是滥用替换回调:

var matches = [];
s.replace(/\[(.*?)\]/g, function(g0,g1){matches.push(g1);})

Example: http://jsfiddle.net/kobi/6CEzP/

示例:http: //jsfiddle.net/kobi/6CEzP/

回答by James Kyburz

var s = 'pass[1][2011-08-21][total_passes]';

r = s.match(/\[([^\]]*)\]/g);

r ; //# =>  [ '[1]', '[2011-08-21]', '[total_passes]' ]

example proving the edge case of unbalanced [];

var s = 'pass[1]]][2011-08-21][total_passes]';

r = s.match(/\[([^\]]*)\]/g);

r; //# =>  [ '[1]', '[2011-08-21]', '[total_passes]' ]

回答by amal

add the global flag to your regex , and iterate the array returned .

将全局标志添加到您的正则表达式,并迭代返回的数组。

 match(/\[(.*?)\]/g)

回答by Chris

I'm not sure if you can get this directly into an array. But the following code should work to find all occurences and then process them:

我不确定您是否可以将其直接放入数组中。但是下面的代码应该可以找到所有出现的情况然后处理它们:

var string = "pass[1][2011-08-21][total_passes]";
var regex = /\[([^\]]*)\]/g;

while (match = regex.exec(string)) {
   alert(match[1]);
}

Please note: i really think you need the character class [^\]] here. Otherwise in my test the expression would match the hole string because ] is also matches by .*.

请注意:我真的认为您需要这里的字符类 [^\]]。否则,在我的测试中,表达式将匹配孔字符串,因为 ] 也与 .* 匹配。

回答by Suriya

[C#]

[C#]

        string str1 = " pass[1][2011-08-21][total_passes]";
        string matching = @"\[(.*?)\]";
        Regex reg = new Regex(matching);
        MatchCollection matches = reg.Matches(str1);

you can use foreach for matched strings.

您可以将 foreach 用于匹配的字符串。