-1

I have this code:

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

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

sawa
  • 165,429
  • 45
  • 277
  • 381
Konstantin
  • 2,983
  • 3
  • 33
  • 55

2 Answers2

1

You need to use this:

puts "1'2".gsub("'","\\\\'")
Momus
  • 394
  • 2
  • 13
0

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

sawa
  • 165,429
  • 45
  • 277
  • 381