Search code examples
rubyescapinggsubapostrophe

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?


Solution

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