Hi! Thanks for visiting my blog. If you've received any value from my content would you mind supporting my new startup by downloading our browser add-on? It's called PriceBlink and makes online shopping a breeze. You can watch it in action here and download it for Chrome, Firefox, IE, or Safari by going to PriceBlink.com. Thank you and I hope you enjoy!

Escaping Single Quotes with Ruby

Dec 31

I ran into a problem this evening when inserting data in MySQL. Some of the data had single quotes and I needed to escape them. One easy one to do this is using the following regular expression:

gsub(/[']/, ‘\\\\\”)
A simple example to run is:

puts “My name’s Dennis”.gsub(/[']/, ‘\\\\\”)

I know there are a couple of methods within the Ruby MySQL module, but I had problems and settled for the example listed above.

Leave a Reply