Reputation: 42003
I need to extract the rightmost parenthetical (the last one) from a string that has 1-N parentheticals.
For example, in Some title (8888)(123, bar)(1000, foo)
I want to get the contents of the last set of parentheses, i.e. 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.
Upvotes: 0
Views: 86
Reputation: 2621
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
Upvotes: 3
Reputation: 2323
you will see with that regex .*\((.+)\)
you can get $1(group one) as your wanted content
Upvotes: 1
Reputation: 368954
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"
Upvotes: 0