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

发布于 2024-12-02 01:12:15 字数 387 浏览 0 评论 0原文

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

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

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

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

console.log(result);

但这只返回[1]

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

I have the following string: 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].

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

水溶 2024-12-09 01:12:15

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

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

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

如果您想要仅捕获组的内容(来自 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]);
}

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

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

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

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

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

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

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

If you want something that only captures the group (from 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/

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/

御弟哥哥 2024-12-09 01:12:15
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]' ]
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]' ]
予囚 2024-12-09 01:12:15

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

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

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

 match(/\[(.*?)\]/g)
弱骨蛰伏 2024-12-09 01:12:15

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

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

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

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

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

猫烠⑼条掵仅有一顆心 2024-12-09 01:12:15
'pass[1][2011-08-21][total_passes]'.match(/\[.+?\]/g); // ["[1]","[2011-08-21]","[total_passes]"]

说明

\[       # match the opening [
           Note: \ before [ tells that do NOT consider as a grouping symbol.
   .+?   # Accept one or more character but NOT greedy
\]       # match the closing ] and again do NOT consider as a grouping symbol
/g       # do NOT stop after the first match. Do it for the whole input string.

您可以使用正则表达式的其他组合
https://regex101.com/r/IYDkNi/1

'pass[1][2011-08-21][total_passes]'.match(/\[.+?\]/g); // ["[1]","[2011-08-21]","[total_passes]"]

Explanation

\[       # match the opening [
           Note: \ before [ tells that do NOT consider as a grouping symbol.
   .+?   # Accept one or more character but NOT greedy
\]       # match the closing ] and again do NOT consider as a grouping symbol
/g       # do NOT stop after the first match. Do it for the whole input string.

You can play with other combinations of the regular expression
https://regex101.com/r/IYDkNi/1

血之狂魔 2024-12-09 01:12:15

[C#]

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

您可以使用 foreach 来匹配字符串。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文