Regular Expression to get a string between parentheses in Javascript

后端 未结 9 801
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 09:45

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 \"(\"

9条回答
  •  鱼传尺愫
    2020-11-22 10:18

    To match a substring inside parentheses excluding any inner parentheses you may use

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

    pattern. See the regex demo.

    In JavaScript, use it like

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

    Pattern details

    • \( - a ( char
    • ([^()]*) - Capturing group 1: a negated character class matching any 0 or more chars other than ( and )
    • \) - a ) char.

    To get the whole match, grab Group 0 value, if you need the text inside parentheses, grab Group 1 value.

    Most up-to-date JavaScript code demo (using 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
    });

    Legacy JavaScript code demo (ES5 compliant):

    var strs = ["I expect five hundred dollars ($500).", "I expect.. :( five hundred dollars ($500)."];
    var rx = /\(([^()]*)\)/g;
    
    
    for (var i=0;i

提交回复
热议问题