我试图编写一个正则表达式,它可以返回括号中的字符串。例如,我想获取位于字符串“(”和“)”之间的字符串。
I expect five hundred dollars ($500).
会返回
$500
在Javascript中获取两个字符串之间的子字符串的正则表达式在这里。
我不知道如何在正则表达式中使用'('和')'。
我试图编写一个正则表达式,它可以返回括号中的字符串。例如,我想获取位于字符串“(”和“)”之间的字符串。
I expect five hundred dollars ($500).
会返回
$500
在Javascript中获取两个字符串之间的子字符串的正则表达式在这里。
我不知道如何在正则表达式中使用'('和')'。
\
进行转义),与原始括号匹配,以及一组常规括号来创建捕获组:
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec("I expect five hundred dollars ($500).");
//matches[1] contains the value between the parentheses
console.log(matches[1]);
分解:
\(
:匹配一个左括号(
:开始捕获组[^)]+
:匹配一个或多个非右括号字符)
:结束捕获组\)
:匹配一个右括号在RegExplained上有一种可视化的解释。
/g
将不能按预期工作。 - silkAdmin尝试字符串操作:
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));
}
简单解决方案
注意:此解决方案仅适用于只包含单个“(”和“)”的字符串,例如此问题中的字符串。
("I expect five hundred dollars ($500).").match(/\((.*)\)/).pop();
为了匹配括号内不包含任何内部括号的子字符串,您可以使用
\(([^()]*)\)
正则表达式。请参见正则表达式演示。
在JavaScript中使用它的方式如下:
var rx = /\(([^()]*)\)/g;
模式细节
\(
- 表示左括号 (
([^()]*)
- 捕获组1:一个否定字符类,匹配除了左右括号 (
和 )
之外的任何0个或多个字符\)
- 表示右括号 )
要获取整个匹配结果,请获取 Group 0 值,如果需要获取圆括号内的文本,请获取 Group 1 值。
最新的 JavaScript 代码演示(使用 matchAll
):
const strs = ["I expect five hundred dollars ($500).", "I expect.. :( five hundred dollars ($500)."];
const rx = /\(([^()]*)\)/g;
strs.forEach(x => {
const matches = [...x.matchAll(rx)];
console.log( Array.from(matches, m => m[0]) ); // All full match values
console.log( Array.from(matches, m => m[1]) ); // All Group 1 values
});
遗留的 JavaScript 代码演示(符合 ES5 标准):
var strs = ["I expect five hundred dollars ($500).", "I expect.. :( five hundred dollars ($500)."];
var rx = /\(([^()]*)\)/g;
for (var i=0;i<strs.length;i++) {
console.log(strs[i]);
// Grab Group 1 values:
var res=[], m;
while(m=rx.exec(strs[i])) {
res.push(m[1]);
}
console.log("Group 1: ", res);
// Grab whole values
console.log("Whole matches: ", strs[i].match(rx));
}
()
及其内容,即使它们并排在一起 ()()
也可以。 - limitlessloopmatchAll
的 JavaScript 代码演示。 - Wiktor Stribiżewconsole.log( Array.from(matches, m => m[0]) ); // All full match values
。你不需要组,可以使用var rx = /\([^()]*\)/g;
。 - Wiktor Stribiżew为了避免使用临时全局变量,将Mr_Green的答案转化为函数式编程风格。
var matches = string2.split('[')
.filter(function(v){ return v.indexOf(']') > -1})
.map( function(value) {
return value.split(']')[0]
})
替代方案:
var str = "I expect five hundred dollars ($500) ($1).";
str.match(/\(.*?\)/g).map(x => x.replace(/[()]/g, ""));
→ (2) ["$500", "$1"]
如果需要,可以用方括号或大括号代替括号。
str
没有括号时,这个东西就坏了。 - Ranjith Varatharajan如果只需要货币符号后面的数字,可以使用\(.+\s*\d+\s*\)
或者对于括号内的任何内容,可以使用\(.+\)
($500)
和(but I'm going to pay)
分别匹配,而不是将它们视为单个匹配项。谢谢! - Tom Birdlet str = "Before brackets (Inside brackets) After brackets".replace(/.*\(|\).*/g, '');
console.log(str) // Inside brackets
var str = "I expect five hundred dollars ($500) ($1).";
var rex = /\$\d+(?=\))/;
alert(rex.exec(str));
将匹配以$开头且后跟')'的第一个数字。 ')'不会成为匹配的一部分。 代码使用第一个匹配项进行警报。
var str = "I expect five hundred dollars ($500) ($1).";
var rex = /\$\d+(?=\))/g;
var matches = str.match(rex);
for (var i = 0; i < matches.length; i++)
{
alert(matches[i]);
}
简单易懂:
(?<value>(?<=\().*(?=\)))
希望我能帮到你。
var regExp = /\(\$([^)]+)\)/;
- Dom Day