gpt4 book ai didi

JavaScript - 找到最后一部分 "foo[biz][bar]"的优雅方式

转载 作者:行者123 更新时间:2023-11-30 07:33:10 24 4
gpt4 key购买 nike

我有一个名称为 "foo[biz][bar]" 的输入。获得最后一部分的优雅方法是什么,即 "bar"?我可以使用 jQuery 和 lodash 库。

最佳答案

你可以使用正则表达式 /\[(.*?)\]/g 获取括号之间的所有匹配项,然后取最后一个匹配项(如果有):

str = "foo[biz][bar]"
matches = str.match(/\[(.*?)\]/g)
if (matches.length) console.log(matches[matches.length - 1])
// based on answer above group override but without `(?:` non capturing group
console.log( /(\[(\w+)\])+/g.exec(str).pop() )

正则表达式 /\[(.*?)\]/g 解释: enter image description here

正则表达式 /(\[(\w+)\])+/g 解释:enter image description here

Debuggex 创建

关于JavaScript - 找到最后一部分 "foo[biz][bar]"的优雅方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43912433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com