Alex Rivera | Logout

Ruby gsub doesn't escape single-quotes

Asked 2010-02-01T21:49:13.527
31

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"
Edit
Report

1 Answer

39

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

"Yaho'o".gsub("'", "\\\\'")
answered 2010-02-01T21:58:21.223

Your Answer