s = "an example with 'one' word and 'two and three' words inside quotes" p s.gsub(/('[^']*')|and/) { $1 || '&' } p s.gsub(/('[^']*')|and/) { |m| m == $~[1] ? $~[1] : '&' }
Standard input is empty
"an example with 'one' word & 'two and three' words inside quotes" "an example with 'one' word & 'two and three' words inside quotes"