-2

I have a string like aaaaaa(bbbbb(ccccccc)dddddd)eeeeeefffff)hhhhh(iiiiii, how to write a Regular Expression which could get bbbbb(ccccccc)dddddd.

In example above, there are two pair of brackets, one pair is in another one. i want get all content in the outer.

I have no idea about it.

Ron
  • 553
  • 2
  • 10
  • 30
  • your example seems unclear for me.. Single example wouldn't be enough give more.. – Avinash Raj Oct 08 '14 at 07:16
  • 2
    Regex is horrible at nesting. Use a tokenizer/parser instead. – h2ooooooo Oct 08 '14 at 07:21
  • @h2ooooooo `Regex is horrible at nesting`. Thanks for your answer. – Ron Oct 08 '14 at 07:35
  • 1
    All answers below are horrible. See [the duplicate](http://stackoverflow.com/a/14952740/), [a perl/pcre solution](http://stackoverflow.com/a/17845034) and [a .net solution](http://stackoverflow.com/a/17004406) – HamZa Oct 08 '14 at 07:38
  • @HamZa yes it's horrible but he fail to explain his question clearly. – Avinash Raj Oct 08 '14 at 07:43
  • @AvinashRaj How come I've found 3 duplicates in no time? – HamZa Oct 08 '14 at 07:44
  • i posted an answer after seeing the accepted answer. I already posted a comment to explain the question clearly http://stackoverflow.com/questions/26251119/how-to-get-all-in-the-most-outer-brackets/26251528#comment41179462_26251119. – Avinash Raj Oct 08 '14 at 07:45
  • A different approach: http://pastebin.com/YHgn6A76 – Viezevingertjes Oct 08 '14 at 08:04
  • @AvinashRaj At first thanks for everybody here answers this question though it is not clearly described. I've searched before I posted my question, because of wrong key words i used, I didn't find useful thing. Maybe It will take days for me to see every link and understand them. It's a wonderful place here indeed. – Ron Oct 09 '14 at 02:07

1 Answers1

0

May not be the best solution, but this might work for your case:

var regex = /\((.*?\(.*?\).*?)\)/;

Here's a snippet to test things out:

    var string = 'aaaaaa(bbbbb(ccccccc)dddddd)eeeeeefffff)hhhhh(iiiiii',
      matches = string.match(/\((.*?\(.*?\).*?)\)/);

    console.log(matches[1]);
gion_13
  • 41,171
  • 10
  • 96
  • 108