I'm trying to reformat a SQL statement (Oracle, fwiw) in Javascript.
I can step through the string char-by-char easy enough, but I want to
do some batch processing up front to cut down on the number of
iterations.
I would like to:
1. replace linebreaks w/ space
2. remove extra whitespace (e.g. replace multiple spaces with a
single space)
3. remove whitespace after words and before commas (e.g.
"someword ," becomes "someword,")
Step 1 is easy, since I don't care about preserving multiline quotes.
(This code won't be executed directly, it's only being formatted for
display.)
But steps 2 and 3 should NOT affect quoted strings, double or single.
I found this regex on
regexlib.com:
(?:\s+)|((?:"(?:.+?)")|(?:'(?:.+?)'))
This does a good job of recognizing the quoted strings, but I'm not
sure exactly how to use it from javascript. This is my first attempt:
alert ("select 'xyz' as \"column_name\" from dual".replace(/(?:
\s+)|((?:"(?:.+?)")|(?:'(?:.+?)'))/g,"+"));
Output:
select+++as+++from+dual
This replaces the non-quoted spaces properly, but it also replaces the
entire quoted strings as if they were spaces. If I could have this
preserve the original quoted strings I would be good to go for steps 2
and 3 above.
Any advice?