Try this Regex (if you don't need javascript regex):
(?
See Explanation and try Demos
You may have some complex texts between (..\n..)
. So I suggest:
1- Add )
in beginning and (
to end of string! wait!
2- split your text with this regex /\)[^\(]*\b(\w+)\b[^\)]*\(/
or execute this regex, two similar ways!
3- you've extract texts out of parenthesis and you can use /\b\w+\b/
to match your words in out of parenthesis in Original text. /\bAPPLE\b/
is for APPLE.
for example:
var mytext = "Long text (for finding APPLE word and maybe replace\n"+
"by bold APPLE or something else!)\n"+
"So if APPLE APPLE and APPLE appear out of parenthesis\n"+
"they should convert to bold APPLE !" ;
mytext = ")" + mytext + "(" ;
var r= new RegExp(/\)[^\(]*\b(\w+)\b[^\)]*\(/g);
var res = mytext.match(r) ;
console.log(res);
//for each matched item in res,
// find APPLE and replace with new value
// by this regex: /\b\w+\b/g
in this case, you will able to ignore APPLE in "(BANANA APPLE what? no! it's not working :P should)"
or more complex texts!