简体   繁体   English

Javascript正则表达式或其他解析技术来提取字符串中最后一组括号的内容

[英]Javascript regex or other parsing technique to extract the contents of last set of parentheses in a string

I need to extract the rightmost parenthetical (the last one) from a string that has 1-N parentheticals. 我需要从具有1-N括号的字符串中提取最右边的括号(最后一个)。

For example, in Some title (8888)(123, bar)(1000, foo) I want to get the contents of the last set of parentheses, ie 1000, foo . 例如,在Some title (8888)(123, bar)(1000, foo)我想获取最后一组括号的内容,即1000, foo There will always be at least one parenthetical, but may be more than one. 总是会有至少一个括号,但可能不止一个。

I am fine with using regex, or other string parsing techniques. 我可以使用正则表达式或其他字符串解析技术。

Assuming they are not nested you can simply do: /\\(([^\\)]+)\\)$/ 假设它们没有嵌套,您可以简单地做: /\\(([^\\)]+)\\)$/

var foo = "Some title (8888)(123, bar)(1000, foo)";
// Get your result with
foo.match(/\(([^\)]+)\)$/)[1];

Demonstration: http://regex101.com/r/tS2yS1 演示: http//regex101.com/r/tS2yS1

follow this link 跟随此链接

you will see with that regex .*\\((.+)\\) you can get $1(group one) as your wanted content 您将看到该正则表达式.*\\((.+)\\)可以获取$ 1(第一组)作为所需内容

Match all parentheticals, and get the last one. 匹配所有括号,并获取最后一个。

> 'Some title (8888)(123, bar)(1000, foo)'.match(/\(.*?\)/g).pop()
"(1000, foo)"

> var x = 'Some title (8888)(123, bar)(1000, foo)'.match(/\(.*?\)/g).pop(); x.substr(1, x.length-2)
"1000, foo"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM