Monday, September 08, 2008

Ruby 正则转义单引号

a = "it's = 'test'"
puts a.gsub(/(')/, '\\\\\\1')

需要将单引号'转义为\',然后存入数据库,本来是用a.gsub(/(')/, "\\'")来处理,发现不正确,查了一下ruby正则,发现:
\' The part of the string after the match
\' 代表是正则匹配位置后面的部分字符串

No comments :