JS Regex用于匹配特定的数组增量,忽略字符串和单独的增量

时间:2022-09-13 16:44:50

I have the following input fields with name attributes of:

我有以下输入字段,其名称属性为:

carousels['components'][0][0][title]
carousels['components'][0][1][title]
carousels['components'][0][2][title]

carousels['components'][1][0][title]
carousels['components'][1][1][title]
carousels['components'][1][2][title]

carousels['components'][2][0][title]
carousels['components'][2][1][title]
carousels['components'][2][2][title]

I am trying to match the final [ number ] eg this part:

我试图匹配最后的[数字],例如这部分:

carousels['components'][2][THIS][title]
carousels['components'][2][THIS][title]
carousels['components'][2][THIS][title]

While ignoring the rest

而忽略了其余的

Here is my regex pattern:

这是我的正则表达式模式:

/(\[[^components\]])+(\[*])/

This affects both of the int's within brackets when I just want the last one. This regex also doesn't recognize the specific requirement of the first array key 'component'

当我只想要最后一个时,这会影响括号内的两个int。此正则表达式也无法识别第一个数组键“组件”的特定要求

Live regex test here:

现场正则表达式测试:

http://www.regexpal.com/?fam=94974

2 个解决方案

#1


1  

If you want to get the last [ + digits + ], you can use

如果你想得到最后一个[+数字+],你可以使用

/^.*\[(\d+)\].*$/

See the regex demo

请参阅正则表达式演示

Backtracking will help getting exactly the last occurrence of [digits]. Grab Group 1 value.

回溯将有助于获得[数字]的最后一次出现。抓住第1组价值。

var re = /^.*\[(\d+)\].*$/; 
var str = 'carousels[\'components\'][0][0][title]\ncarousels[\'components\'][0][1][title]\ncarousels[\'components\'][0][2][title]\n\ncarousels[\'components\'][1][0][title]\ncarousels[\'components\'][1][1][title]\ncarousels[\'components\'][1][2][title]\n\ncarousels[\'components\'][2][0][title]\ncarousels[\'components\'][2][1][title]\ncarousels[\'components\'][2][2][title]';

for (var s of str.split("\n")) {
    var res = (m=re.exec(s)) ? m[1] : "";
    if (res) {
      document.body.innerHTML += s + ": " + res + "<br/>";
    }
}

UPDATE:

To get the first [ + digits + ], you need to use lazy matching with the first dot:

要获得第一个[+ digits +],您需要使用与第一个点的延迟匹配:

/^.*?\[(\d+)\].*$/
    ^ - Here, the ? will make matching lazy/reluctant 
        (it will match any 0+ chars other than a newline as few as possible)

See another regex demo.

看另一个正则表达式演示。

#2


1  

You can try this

你可以试试这个

    ^.*(\[.*?\])\[.*?\]$
       <------->
Match in this(1st captured group)

Regex Demo

If you want to match ['components'] exclusively, then you can use

如果您想要独占匹配['components'],那么您可以使用

^.*\['components'\].*(\[.*?\])\[.*?\]$

#1


1  

If you want to get the last [ + digits + ], you can use

如果你想得到最后一个[+数字+],你可以使用

/^.*\[(\d+)\].*$/

See the regex demo

请参阅正则表达式演示

Backtracking will help getting exactly the last occurrence of [digits]. Grab Group 1 value.

回溯将有助于获得[数字]的最后一次出现。抓住第1组价值。

var re = /^.*\[(\d+)\].*$/; 
var str = 'carousels[\'components\'][0][0][title]\ncarousels[\'components\'][0][1][title]\ncarousels[\'components\'][0][2][title]\n\ncarousels[\'components\'][1][0][title]\ncarousels[\'components\'][1][1][title]\ncarousels[\'components\'][1][2][title]\n\ncarousels[\'components\'][2][0][title]\ncarousels[\'components\'][2][1][title]\ncarousels[\'components\'][2][2][title]';

for (var s of str.split("\n")) {
    var res = (m=re.exec(s)) ? m[1] : "";
    if (res) {
      document.body.innerHTML += s + ": " + res + "<br/>";
    }
}

UPDATE:

To get the first [ + digits + ], you need to use lazy matching with the first dot:

要获得第一个[+ digits +],您需要使用与第一个点的延迟匹配:

/^.*?\[(\d+)\].*$/
    ^ - Here, the ? will make matching lazy/reluctant 
        (it will match any 0+ chars other than a newline as few as possible)

See another regex demo.

看另一个正则表达式演示。

#2


1  

You can try this

你可以试试这个

    ^.*(\[.*?\])\[.*?\]$
       <------->
Match in this(1st captured group)

Regex Demo

If you want to match ['components'] exclusively, then you can use

如果您想要独占匹配['components'],那么您可以使用

^.*\['components'\].*(\[.*?\])\[.*?\]$