tokhi
tokhi

Reputation: 21618

escape double quotes in ruby

How can I replace double-quotes to &quote; in a string? This is what I have tried:

1.9.3-p362 :009 > a =  "\"That's it\", she said." 
 => "\"That's it\", she said." 
1.9.3-p362 :010 > a.tr('"', "&quote;")
 => "&That's it&, she said." 

As you see instead of &quotes; I only get &, any idea?

Upvotes: 1

Views: 4126

Answers (1)

davegson
davegson

Reputation: 8331

use gsub instead

a.gsub(/\"/, "&quote;")

# without regex as noted by hirolau
a.gsub("\"", "&quote;")

# => "&quote;That's it&quote;, she said."

Upvotes: 4

Related Questions