I would like to replace extra spaces (instances of consecutive whitespace characters) with one space, as long as those extra spaces are not in double or single quotes (or any other enclosures I may want to include).
I saw some similar questions, but I could not find a direct response to my needs above. Thank you!
Hope you’re still looking, or come back to check! This seems to work for me:
…and replace with
$1EDIT
Also, if you need to allow for escaped quotes like
\"or\', you could use this expression:It gets a bit stickier if you want to add support for “balanced” quotes like brackets (e.g.
()or{})END EDIT
Let me know if you find problems or would like some explanation!
HOPEFULLY FINAL EDIT AND WARNINGS
/(?:^|\s+)((["\'])(\\\\\2|(?!\2).)*?(?=\2)\2)|\s\s+/\s+with(?:^|\s+)at the beginning of the expression.and\sshorthand character classes with the specific characters you want to match, like the following:/(?:^|[ \t]+)((["\'])(\\\\\2|(?!\2)[\s\S])*?(?=\2)\2)|[ \t]{2,}/EXAMPLE
This link shows an example of the first expression and last expression in use on sample text on http://codepad.viper-7.com