From: Matt Date: 2008-05-18T16:55:07+09:00 Subject: Re: sub/gsub bug? J. Cooper wrote: > The gsub replacement string is a crazy thing when it comes to escaping, > because remember you can put backreferences in it. Any double quoted > string needs 2 backslashes to represent a literal backslash, but > remember in this context, a literal backslash represents the > backreference syntax, so you need to escape it with another "literal > backslash" of 2 backslashes. > > At least, I think that's how I understand it :P Yeah, I get all that, but I didn't think that \' was a backreference. I thought it was limited to numbers and the ampersand. But now that I think about it, Perl has a $' variable that contains the portion of the string after the match, so I guess that's where the idea came from. I've never seen that in backreference form before. irb(main):005:0> "matt's test".gsub(/'/, "\\`") => "mattmatts test" irb(main):006:0> "matt's test".gsub(/'/, "\\&") => "matt's test" irb(main):007:0> "matt's test".gsub(/'/, "\\'") => "matts tests test" Yay! Matt