Q:

RegExp - find all occurences, but not inside quotes

I have this text (it's a string value, not a language expression):
hello = world + 'foo bar' + gizmo.hoozit + "escaped \"quotes\"";

And I would like to find all words ([a-zA-Z]+) which are not enclosed in double or single quotes. The quotes can be escaped (\" or \'). The result should be:
hello, world, gizmo, hoozit

Can I do this using regular expressions in JavaScript?

A:

you can use this pattern, what you need is in the second capturing group:
EDIT: a little bit shorter with a negative lookahead:
var re = /(['"])(?:[^"'\\]+|(?!\1)["']|\\{2}|\\[\s\S])*\1|([a-z]+)/ig

var mystr = 'hello = world + \'foo bar\' + gizmo.hoozit + "escaped \\"quotes\\"";';

var result = Array();
while (match = re.exec(mystr)) {
    if (match[2]) result.push(match[2]);
}

console.log(mystr);
console.log(result);

the idea is to match content enclosed between quotes before the target.
Enclosed content details: '(?:[^'\\]+|\\{2}|\\[\s\S])*'
(["'])         # literal single quote
(?:            # open a non capturing group
    [^"'\\]+   # all that is not a quote or a backslash
  |            # OR
    (?!\1)["'] # a quote but not the captured quote
  |            # OR
    \\{2}      # 2 backslashes (to compose all even numbers of backslash)*
  |            # OR
    \\[\s\S]   # an escaped character (to allow escaped single quotes)
)*             # repeat the group zero or more times
\1             # the closing single quote (backreference)

(* an even number of backslashes doesn't escape anything)

