1

如何在字符串中替换double-quotes为?&quote;这是我尝试过的:

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." 

如您所见,而不是&quotes;我只得到&,知道吗?

4

1 回答 1

4

gsub改为使用

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

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

# => "&quote;That's it&quote;, she said."
于 2013-11-08T12:24:44.260 回答