In message <11*********************@f16g2000cwb.googlegroups. com>, Thu,
9 Nov 2006 22:20:01, Peter Michaux <pe**********@gmail.comwrites
>
Thanks for the suggestion. I think that if I allow nested parens then
this won't work. In the following "test two" looks like it is outside
some brackets.
((test one) test two (test three)) test four
I think that my whole idea has gone down the tubes. I need to write a
tokenizer and a real parser.
Consider looping on replacing ( any number of not ( or ) ) by
space until there was no change, then doing a simple search on the
residue. Undertested :-
St = "((test one) test two (test three)) test four"
function RP(S) { return S == (S=S.replace(/\([^()]*\)/g, " ")) ? S :
RP(S) }
Answer = RP(St).match(/\btest\s+(\S+)/)[1]
The g is optional.
The \b may need more thought, if "()test a" and other odd cases are
to be matched.
The code needs error-detection for the case where "test word" is not
found.
To be really clever, adapt it to work with matching () {} []
using only a single RegExp and .replace .
It's a good idea to read the newsgroup and its FAQ. See below.
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.