正则表达式以获取Javascript中括号之间的字符串我正在尝试编写一个正则表达式,它返回一个字符串,该字符串位于括号之间。例如:我希望得到驻留在字符串“(”和“)之间的字符串。I expect five hundred dollars ($500).会回来$500发现正则表达式以获取Javascript中两个字符串之间的字符串但我是新来的regex。我不知道如何在regexp中使用‘(’,‘)
3 回答
慕婉清6462132
TA贡献1804条经验 获得超2个赞
\
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec("I expect five hundred dollars ($500).");
//matches[1] contains the value between the parentheses
console.log(matches[1]);
\(:匹配开头括号 (*开始捕获组 [^)]+:匹配一个或多个非 )人物 )*终端捕获小组 \):匹配结束括号
慕无忌1623718
TA贡献1744条经验 获得超4个赞
var txt = "I expect five hundred dollars ($500). and new brackets ($600)";var newTxt = txt.split('(');for (var i = 1; i < newTxt.length; i++) {
console.log(newTxt[i].split(')')[0]);}var txt = "I expect five hundred dollars ($500). and new brackets ($600)";var regExp = /\(([^)]+)\)/g;var matches = txt.match(regExp);for (var i = 0; i < matches.length; i++) {
var str = matches[i];
console.log(str.substring(1, str.length - 1));}添加回答
举报
0/150
提交
取消
