I’m writing a regular expression in javascript that replaces whitespaces except when:
- Some specific syntax is in front of the whitespace
- It’s surrounded in both single as double quotes (escaped quotes within quotes excluded)
Now, I’ve got a big part working. It matches all patterns that doesn’t have the specific syntax in front of the whitespace, however, I’m stuck with the quote part.
return str.replace(/(function|new|return|var)?\s/g, function($0, $1) {
return $1 ? $0 : '';
});
I’ve done quite some testing, but I just can’t figure it out. Thanks in advance.
You can use:
See http://jsfiddle.net/qCeC4/
Lookahead part in Perl
/xform:Note: As I said before, this is not a good way to parse JS, and will break on comments, regex quoting, and who knows what else.
Note2: Forgot to add that this only works for “valid” quoting, all quotes must be closed.