正则表达式,在Javascript中获取圆括号之间的字符串

时间:2021-01-25 22:25:22

I am trying to write a regular expression which returns a string which is between parentheses. For example: I want to get the string which resides between the strings "(" and ")"

我正在尝试编写一个正则表达式,它返回圆括号之间的字符串。例如:我想获得位于字符串之间的字符串"("和")"

I expect five hundred dollars ($500).

would return

将返回

$500

Found Regular Expression to get a string between two strings in Javascript

在Javascript中找到正则表达式以在两个字符串之间获取字符串

But I'm new with regex. I don't know how to use '(', ')' in regexp

但我是regex的新手。我不知道如何在regexp中使用'(',')'

6 个解决方案

#1


325  

You need to create a set of escaped (with \) parentheses (that match the parentheses) and a group of regular parentheses that create your capturing group:

您需要创建一组转义(带有)圆括号(与圆括号匹配)和一组常规圆括号,以创建捕获组:

var regExp = /\(([^)]+)\)/;
var matches = regExp.exec("I expect five hundred dollars ($500).");

//matches[1] contains the value between the parentheses
console.log(matches[1]);

Breakdown:

分解:

  • \( : match an opening parentheses
  • (:用圆括号括起
  • ( : begin capturing group
  • (:开始抓捕小组
  • [^)]+: match one or more non ) characters
  • (^))+:匹配一个或多个非)字符
  • ) : end capturing group
  • ):终端捕获组
  • \) : match closing parentheses
  • 圆括号匹配

Here is a visual explanation on RegExplained

这里有一个关于regexplain的可视化解释

#2


23  

Try string manipulation:

试试字符串操作:

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]);
}

or regex (which is somewhat slow compare to the above)

或者regex(与上面的相比有点慢)

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));
}

#3


5  

Ported Mr_Green's answer to a functional programming style to avoid use of temporary global variables.

移植Mr_Green对函数式编程风格的回答,以避免使用临时全局变量。

var matches = string2.split('[')
  .filter(function(v){ return v.indexOf(']') > -1})
  .map( function(value) { 
    return value.split(']')[0]
  })

#4


3  

For just digits after a currency sign : \(.+\s*\d+\s*\) should work

对于货币符号后的数字:\(.+\s*\d+\s*\ s*\)应该有效

Or \(.+\) for anything inside brackets

或\(.+\)用于括号内的任何东西

#5


1  

Simple solution

简单的解决方案

Notice: this solution used for string that has only single "(" and ")" like string in this question.

注意:这个解决方案用于在这个问题中只有一个像字符串一样的“(”和“)”的字符串。

("I expect five hundred dollars ($500).").match(/\((.*)\)/).pop();

Online demo (jsfiddle)

在线演示(jsfiddle)

#6


0  

var str = "I expect five hundred dollars ($500) ($1).";
var rex = /\$\d+(?=\))/;
alert(rex.exec(str));

Will match the first number starting with a $ and followed by ')'. ')' will not be part of the match. The code alerts with the first match.

将匹配第一个数字,从$开始,然后是')'。)“不会参加比赛。”代码在第一次匹配时发出警报。

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]);
}

This code alerts with all the matches.

此代码与所有匹配一起发出警报。

References:

引用:

search for "?=n" http://www.w3schools.com/jsref/jsref_obj_regexp.asp

搜索“?= n“http://www.w3schools.com/jsref/jsref_obj_regexp.asp

search for "x(?=y)" https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp

搜索“x(? = y)https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp

#1


325  

You need to create a set of escaped (with \) parentheses (that match the parentheses) and a group of regular parentheses that create your capturing group:

您需要创建一组转义(带有)圆括号(与圆括号匹配)和一组常规圆括号,以创建捕获组:

var regExp = /\(([^)]+)\)/;
var matches = regExp.exec("I expect five hundred dollars ($500).");

//matches[1] contains the value between the parentheses
console.log(matches[1]);

Breakdown:

分解:

  • \( : match an opening parentheses
  • (:用圆括号括起
  • ( : begin capturing group
  • (:开始抓捕小组
  • [^)]+: match one or more non ) characters
  • (^))+:匹配一个或多个非)字符
  • ) : end capturing group
  • ):终端捕获组
  • \) : match closing parentheses
  • 圆括号匹配

Here is a visual explanation on RegExplained

这里有一个关于regexplain的可视化解释

#2


23  

Try string manipulation:

试试字符串操作:

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]);
}

or regex (which is somewhat slow compare to the above)

或者regex(与上面的相比有点慢)

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));
}

#3


5  

Ported Mr_Green's answer to a functional programming style to avoid use of temporary global variables.

移植Mr_Green对函数式编程风格的回答,以避免使用临时全局变量。

var matches = string2.split('[')
  .filter(function(v){ return v.indexOf(']') > -1})
  .map( function(value) { 
    return value.split(']')[0]
  })

#4


3  

For just digits after a currency sign : \(.+\s*\d+\s*\) should work

对于货币符号后的数字:\(.+\s*\d+\s*\ s*\)应该有效

Or \(.+\) for anything inside brackets

或\(.+\)用于括号内的任何东西

#5


1  

Simple solution

简单的解决方案

Notice: this solution used for string that has only single "(" and ")" like string in this question.

注意:这个解决方案用于在这个问题中只有一个像字符串一样的“(”和“)”的字符串。

("I expect five hundred dollars ($500).").match(/\((.*)\)/).pop();

Online demo (jsfiddle)

在线演示(jsfiddle)

#6


0  

var str = "I expect five hundred dollars ($500) ($1).";
var rex = /\$\d+(?=\))/;
alert(rex.exec(str));

Will match the first number starting with a $ and followed by ')'. ')' will not be part of the match. The code alerts with the first match.

将匹配第一个数字,从$开始,然后是')'。)“不会参加比赛。”代码在第一次匹配时发出警报。

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]);
}

This code alerts with all the matches.

此代码与所有匹配一起发出警报。

References:

引用:

search for "?=n" http://www.w3schools.com/jsref/jsref_obj_regexp.asp

搜索“?= n“http://www.w3schools.com/jsref/jsref_obj_regexp.asp

search for "x(?=y)" https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp

搜索“x(? = y)https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp