本文介绍了使用Gsub逃避使徒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ruby,并且尝试将'字符转义为\',以便可以在SQL中使用它们.我正在尝试使用gsub,但是它似乎没有用.

I'm working in Ruby and I'm trying to escape ' characters to \' so that I can use them in SQL. I'm trying to use gsub, but it doesn't seem to be working.

"this doesn't work".gsub /'/, '\\'' #=> "this doesnt workt work"
"this doesn't work".gsub /'/, '\\\'' #=> "this doesnt workt work"
"this doesn't work".gsub /'/, '\\\\'' #=> "this doesn\\'t work"
"this doesn't work".gsub /'/, '\\\\\'' #=> "this doesn\\'t work"

我不知道gsub是否是正确的使用方法,所以我愿意尝试几乎所有能够获得所需结果的方法.

I don't know if gsub is even the right method to be using, so I'm willing to try almost anything that gets the results I'm looking for.

推荐答案

由于Ruby的正则表达式中的特殊含义/解释,其他人遇到了这个问题.

Someone else had this very issue, due to a special meaning/interpretation in Ruby's regex.

请参见此答案.

这行吗?

"this doesn't work".gsub /'/, '\\\\\'' => "this doesn\\'t work"

这篇关于使用Gsub逃避使徒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:30