我是正则表达式的新手。我正在尝试使用 JavaScript 正则表达式从字符串中提取括号内的最后一个子字符串。它不适合我。相反,我得到了一对括号内的第一个子字符串。有人能帮我吗?
这是一个例子。我正在尝试提取子字符串'xyz',但我得到的是'abc'。
var str1 = 'Hello World (abc)'; // May or may not contain parentheses
var str2 = '(xyz)'; // Definitely contains parentheses
var troublesomeString = str1 + ' ' + str2; // This is the string I'm working with
var result = myFunc(troublesomeString);
alert(result); // Should say 'xyz', but instead says 'abc'
...
function myFunc(troublesomeString) {
// Here I am trying to get '(xyz)', but am instead getting '(abc) (xyz)'
var resultArray = troublesomeString.match(/\(.+\)$/);
troublesomeString = resultArray[0];
// Here I am trying to get 'xyz', but am instead getting 'abc'
resultArray = troublesomeString.match(/[a-z]+/); // Adding a $ after + doesn't help
troublesomeString = resultArray[0]; // resultArray[1] is null
return troublesomeString;
}