javascript - Match last occuring enclosing outer brackets -


i tried 3 hours construct following regex match without success. have following 2 strings:

this test string illustrate problem (example) in complex matching logic (work / not working (in case) match last occurring bracket closure) 

and

simpler version of string (matchable in easy way) 

i define str.match() matches last part of strings above. resulting in:

work / not working (in case) match last occurring bracket closure 

and

matchable in easy way 

any way achieve this? sadly data highly volatile strong regex rather preferred instead of long functional logic. much!

you can’t match arbitrarily nested parentheses regular expressions. regular expression engines have been extended in such way can parse kind of grammar, javascript’s has not; you’ll need match manually.

function lastparenthesizedsubstring(text) {     var end = text.lastindexof(')');     var = end - 1;     var nesting = 1;      while (nesting > 0) {         if (i < 0) {             // parenthesis imbalance! may want throw             // exception or something.             return null;         }          var c = text.charat(i);          if (c === ')') {             nesting++;         } else if (c === '(') {             nesting--;         }          i--;     }      return text.substring(i + 2, end); } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -