I am trying to find a regex that will do the following (works in Javascript). I want to take a string containing some type markers (token)inside parentheses. My goal is to capture tokens (including parentheses). I assume that parenthese are not nested and that every open parenthesis is ultimately closed.
The regular expression that I would use
[[^\(\)]*|(\(.*?\))]*
Let me break it:
[
[^\(\)]*
|
(
\(.*?\)
)
]*
Needless to say, this will not work (why am I asking here differently?):
var a = /[[^\(\)]*|(\(.*?\))]*/;
a.exec('foo(bar)');
This does not work in both Firefox and Node. My previous attempt was a bit more complex regex:
(?:[^\(\)]*(\(.*?\)))*[^\(\)]*
which can be described as follows
(?:
[^\(\)]*
(\(.*?\))
)*
[^\(\)]*
This will work on foo(bar), but will not work on foo(bar)(quux), if there is only quux.
?