Ruby gsub不能单引号 [英] Ruby gsub doesn't escape single-quotes

查看:172
本文介绍了Ruby gsub不能单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这里发生了什么。我应该如何提供gsub来获取字符串Yaho\'o?

 >> Yaho'o.gsub(Y,\\\Y)
=> \\Yaho'o
>> Yaho'o.gsub(',\\\)
=> Yahooo


解决方案

\'比赛后的一切。
再次删除\它的作品

 Yaho'o.gsub(', \\\')


I don't understand what is going on here. How should I feed gsub to get the string "Yaho\'o"?

>> "Yaho'o".gsub("Y", "\\Y")
=> "\\Yaho'o"
>> "Yaho'o".gsub("'", "\\'")
=> "Yahooo"

解决方案

\' means $' which is everything after the match. Escape the \ again and it works

"Yaho'o".gsub("'", "\\\\'")

这篇关于Ruby gsub不能单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆