RegEx to match stuff between parentheses

153,015

Solution 1

You need to make your regex pattern 'non-greedy' by adding a '?' after the '.+'

By default, '*' and '+' are greedy in that they will match as long a string of chars as possible, ignoring any matches that might occur within the string.

Non-greedy makes the pattern only match the shortest possible match.

See Watch Out for The Greediness! for a better explanation.

Or alternately, change your regex to

\(([^\)]+)\)

which will match any grouping of parens that do not, themselves, contain parens.

Solution 2

Use this expression:

/\(([^()]+)\)/g

e.g:

function()
{
    var mts = "something/([0-9])/([a-z])".match(/\(([^()]+)\)/g );
    alert(mts[0]);
    alert(mts[1]);
}

Solution 3

If s is your string:

s.replace(/^[^(]*\(/, "") // trim everything before first parenthesis
 .replace(/\)[^(]*$/, "") // trim everything after last parenthesis
 .split(/\)[^(]*\(/);      // split between parenthesis

Solution 4

var getMatchingGroups = function(s) {
  var r=/\((.*?)\)/g, a=[], m;
  while (m = r.exec(s)) {
    a.push(m[1]);
  }
  return a;
};

getMatchingGroups("something/([0-9])/([a-z])"); // => ["[0-9]", "[a-z]"]
Share:
153,015
Oscar Godson
Author by

Oscar Godson

Fork me on Github: http://github.com/oscargodson Read my stuff: http://oscargodson.com

Updated on July 05, 2022

Comments

  • Oscar Godson
    Oscar Godson almost 2 years

    I'm having a tough time getting this to work. I have a string like:

    something/([0-9])/([a-z])
    

    And I need regex or a method of getting each match between the parentheses and return an array of matches like:

    [
      [0-9],
      [a-z]
    ]
    

    The regex I'm using is /\((.+)\)/ which does seem to match the right thing if there is only one set of parenthesis.

    How can I get an array like above using any RegExp method in JavaScript? I need to return just that array because the returned items in the array will be looped through to create a URL routing scheme.

  • Oscar Godson
    Oscar Godson almost 13 years
    Perfect, that matches it! but im stuck at how to get make an array from that? I just get ["([0-9])"] with str.match(/\([^\)]+\)/g) :(
  • Rob Raisch
    Rob Raisch almost 13 years
    You're missing the internal start and end capture parens. Try /\(([^)]+)\)/g which will capture all possible parenthesized values. Note the second pair of parens after \( and before \).
  • Dmitri
    Dmitri over 8 years
    Thanks for the "greedy" part. Helped a lot.
  • Aryan Firouzian
    Aryan Firouzian over 8 years
    In my case to include parens that do not contain any other parens, I had to add open parens to the regex: \(([^\)(]+)\)
  • adrianmc
    adrianmc over 7 years
    This is great, but is there a way to not match the actual parens themselves? I can always remove the first and last character of every match, but it would be nice if it could all be done in one line.
  • Rob Raisch
    Rob Raisch over 7 years
    The simplest way would be to do a replace and take the result, as in: result=str.replace(/^.+?\(([^\)]+)\).+$/,'$1') which will replace a string that contains a parenthesized span of text with the text itself. See jsfiddle.net/b3rs34d4
  • Rob Raisch
    Rob Raisch over 7 years
    And here's a one-liner that extracts all parenthesized values from a multi-lined string... jsfiddle.net/pv8pzL6j
  • Rob Raisch
    Rob Raisch over 7 years
    And just for the record, for complex extractions I've found regexen to be of limited value due to their complexity and lack of easy readability. Adding multi-line, free-whitespaced and commented sugar like XRegExp helps a lot, but I've found writing a job-specific parser using PegJS to be invaluable. I'm happy to explain more if anyone's interested.