I have a string like this:
a b c a b \" a b \" b a \" a \"
How do I match every a
that is not part of a string deli
js-coder, resurrecting this ancient question because it had a simple solution that wasn't mentioned. (Found your question while doing some research for a regex bounty quest.)
As you can see the regex is really tiny compared with the regex in the accepted answer: ("[^"]*")|a
subject = 'a b c a b " a b " b a " a "'
regex = /("[^"]*")|a/
replaced = subject.gsub(regex) {|m|$1}
puts replaced
See this live demo
Reference
How to match pattern except in situations s1, s2, s3
How to match a pattern unless...