Javascript 用于匹配特定数组增量的JS正则表达式忽略字符串和单独增量

Javascript 用于匹配特定数组增量的JS正则表达式忽略字符串和单独增量,javascript,html,arrays,regex,preg-match,Javascript,Html,Arrays,Regex,Preg Match,我有以下名称属性为的输入字段: 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[

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

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]
我正在尝试匹配最终的[数字],例如这部分:

carousels['components'][2][THIS][title]
carousels['components'][2][THIS][title]
carousels['components'][2][THIS][title]
而忽略了其余的

以下是我的正则表达式模式:

/(\[[^components\]])+(\[*])/
当我只需要最后一个int时,这会影响括号内的两个int。此正则表达式也不识别第一个数组键“component”的特定要求

在此处进行实时正则表达式测试:

你可以试试这个

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

如果要获取最后的
[
+
数字
+
]
,可以使用

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

回溯将有助于准确获取上次出现的
[数字]
。抓取组1的值

var re=/^.*\[(\d+)\].*$/;
var str='carousels[\'components\'][0][0][title]\ncarousels[\'components\'][0][1][title]\ncarousels[\'components\'][0][1][title]\ncarousels[\'components\'][1][title]\ncarousels[\'components\'][1][2][0][title]\ncarousels[\'components\'components\'][2][1][title][[\'components\'][2][2][title];
对于(str.split(“\n”)的变量s){
var res=(m=re.exec))?m[1]:“”;
如果(res){
document.body.innerHTML+=s+“:“+res+”
”; }
}
谢谢这是完美的,尽管我刚刚意识到我需要检索第一个[数字]而不是第二个数字。只是摆弄一下regexThen使用
*?
开始。我添加了这个regex并做了一些解释。谢谢你的帮助-贪婪在这种情况下不起作用(使用替换方法)-我可以从['components']开始字符串搜索吗?我不太清楚您的输入,如果它是多行的,您可以尝试
。替换(/^(.*.\['components'\]\[)\d+\]/gm,$12]')
。看,完美-是我的替代品不正确。非常感谢