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

时间:2022-07-17 21:45:47

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(/\[(.*?)\]/);

比赛(/\[(。*?)\]/);

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.

不知道怎么做...提前谢谢。

5 个解决方案

#1


29  

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

你几乎就在那里,你只需要一个全局匹配(注意/ 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/

#2


4  

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

#3


1  

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

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

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

#4


0  

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 .*.

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

#5


-1  

[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来匹配字符串。

#1


29  

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

你几乎就在那里,你只需要一个全局匹配(注意/ 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/

#2


4  

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

#3


1  

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

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

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

#4


0  

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 .*.

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

#5


-1  

[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来匹配字符串。