Topic: how to progamatically esacape double quoutes " in a string

Hi, I have a string that comes from a form, I think the active record/controller escapes anything that comes in, but saves as the original in the database. When I eval the string if it has double quotes in it unescaped it errors out.

Anyway, assum I have a string "balbabbabab\"dfadfafdadfdfa"adfdfadfaf" and I want to make it
"balbabbabab\"dfadfafdadfdfa\"adfdfadfaf".

I tried,
mystring = mystring.gsub(/"/,'\"')

but this replaces all double quotes with \" , so I want to just escape any double quote not already escaped (assuming they may be escaped sometimes and may not be othertimes)

Im sure there is some regex or other ruby magic I could do this real easy smile

thanks
Joel

Re: how to progamatically esacape double quoutes " in a string

Try this:

mystring = mystring.gsub(/\\?"/,'\"')

But out of curiosity, why are some quotes in the string escaped while others aren't?

Railscasts - Free Ruby on Rails Screencasts

Re: how to progamatically esacape double quoutes " in a string

Double quotes are escaped with \.
puts "Hello \"Ruby\""
outputs Hello "Ruby"

Re: how to progamatically esacape double quoutes " in a string

Thanks Ryan that worked!, its an admin tool for my site, the text based links we use for affiliates sometimes are escaped and sometimes are not, I do some variable substitution so if I allow the unescaptted urls in, when I do eval on them they crap out.