正则表达式以匹配最后一对括号中的内容

Regex to match contents in last pair of brackets

本文关键字:最后 正则表达式      更新时间:2023-09-26

使用 JavaScript 正则表达式,如何匹配最后一组括号中的内容?

input = "the string (contains) things like (35) )( with (foobar) and )( stuff";
desired_output = "foobar"

我尝试过的所有东西都过度匹配或完全失败。 任何帮助将不胜感激。

谢谢。

一种选择是匹配不包含其他括号的括号,例如:

var tokens = input.match(/'([^()]*')/g);
var last = tokens.pop();

或者作为一个行...

var last = input.match(/'([^()]*')/g).pop().replace(/('(|'))/g,"").replace(/(^'s+|'s+$)/g,"");
相关文章: