Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图得到最后一次发生的情况。
obj.foo[0].bar[0].name
我尝试了这段代码,但只是得到了第一个[0],如果我把它放在$最后就行不通了。
[0]
$
\[(\d+)\](?:(?!\[\d+\]))
你必须断言另一个同组不能在比赛之后的某个地方,而不仅仅是在比赛之后。看到这个修改:
\[(\d+)\](?!.*?\[\d+\]) ^^^
或者您可以切换到回溯方法:
.*\[(\d+)\]
这是第一个正则表达式的正则表达式演示,这是另一个。
根据您是否想要包含括号,您可以使用以下内容:
var r = s.match(/\[\d+\]/g)[1]
如果您不想要分隔符,您可以从组索引中分割它们。
var r = s.match(/\[\d+\]/g)[1].slice(1,-1)