Konstantin Konstantin - 1 month ago 8
Ruby Question

When trying to escape apostrophe with 'gsub', I get backreference

I have this code:

"1'2".gsub("'","\\'")


Instead of
"1\'2"
, I get:
"122"
. Why?

Answer Source

It is because "\\'" means the context following the match, which is "2".