Farhan Salam Farhan Salam - 1 year ago 72
Ruby Question

Replace inner double quotes in a string in an array

I have strings in an array:


I am using
method to form an array with this string, but the inner double quotes are not escaped:

eval('["foo","bar","foo"bar"baz"]') # => SyntaxError

Is there a way to escape only the ones inside an array element?

Answer Source

I think you can try something like

eval('["foo","bar","foo"bar"baz"]'.gsub(/(\w)\"(\w)/, '\1\"\2'))

if I got what you have meant

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download