替换“&”到“\&”在Ruby中似乎不可能? [英] Replace "&" to "\&" in Ruby seems impossible?
本文介绍了替换“&”到“\&”在Ruby中似乎不可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将所有&
字符替换为 \&
与 String .gsub
(或其他方法)。我已经尝试了几种组合,并阅读另一个问题在这里,但什么都不会奏效。
I want to replace all &
characters into \&
with String.gsub
(or a other method). I've tried several combinations and read another question here, but nothing is gonna work.
"asdf & asdf".gsub("&", "\\\&") => "asdf & asdf"
推荐答案
您链接的问题提供解决方案 - 使用 gsub
的块形式:
Your linked question provides a solution - use the block form of gsub
:
irb(main):009:0> puts "asdf & asdf".gsub("&"){'\&'}
asdf \& asdf
这篇关于替换“&”到“\&”在Ruby中似乎不可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文