首页 文章

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

提问于
浏览
128

我正在尝试编写一个正则表达式,它返回括号之间的字符串 . 例如:我想获取字符串“(”和“)”之间的字符串

I expect five hundred dollars ($500).

会回来的

$500

找到Regular Expression to get a string between two strings in Javascript

但我是正则表达式的新手 . 我不知道如何在regexp中使用'(',')'

7 回答

  • 7

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

    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%252F)的视觉解释

  • 363

    尝试字符串操作:

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

    或正则表达式(有点slow compare to the above

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

    Mr_Green's answer移植到函数式编程样式以避免使用临时全局变量 .

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

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

    \(.+\) 括号内的任何内容

  • 0

    Simple solution

    Notice :此解决方案用于此问题中仅包含单个"("和")"字符串的字符串 .

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

    Online demo (jsfiddle)

  • 2

    要匹配括号内的子字符串,不包括您可能使用的任何内括号

    \(([^()]*)\)
    

    图案 . 见the regex demo .

    在JavaScript中,使用它

    var rx = /\(([^()]*)\)/g;
    

    Pattern details

    • \( - 一个 ( char

    • ([^()]*) - 捕获组1:negated character class匹配 () 以外的任何0或更多字符

    • \) - 一个 ) char .

    要获得整个匹配,请抓取Group 0值,如果需要括号内的文本,请获取Group 1值:

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

    此代码会提醒所有匹配项 .

    参考文献:

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

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

相关问题