1

I simply don't understand if this behavior is expected or not.

I am trying to replace the ' character in a string by the 2 characters \' (basically escaping the quote by adding a \ before it)

My naive approach was this str.gsub("'", "\\'") but I got weirds results. I finally got it to work by using str.gsub("'") { "\\'" } but I would like to understand the behavior of gsub here.

Here is what I tried :

str2 = "super'test"
 => "super'test"
str2.gsub("'", "\\'")
 => "supertesttest"                # <==== ??? Where does that comes from ?

# Further tests are to understand if `gsub` behaves as I would expect, and it does...
str2.gsub("'", "")
 => "supertest"
str2.gsub("'", "\\")
 => "super\\test"
str2.gsub("t", "ab")
 => "super'abesab"
str2.gsub("t", "\b")
 => "super'\bes\b"
str2.gsub("t", "\\b")
 => "super'\\bes\\b"
str2.gsub("t", "\'")
 => "super''es'"
str2.gsub("'", "\'")
 => "super'test"
str2.gsub("'", "\\ '")
 => "super\\ 'test"

str2.gsub("'", "\\'")
 => "supertesttest" # I really don't get it....
str2.gsub("'") { "\\'" }
 => "super\\'test" # This works
Azyrod
  • 57
  • 5

1 Answers1

2

Quote from the docs:

\' corresponds to $', which contains string after match.

[...]

Note that \\ is interpreted as an escape, i.e., a single backslash.

[...]

If you want to write a non-back-reference string \& in replacement, you need first to escape the backslash to prevent this method from interpreting it as a back-reference, and then you need to escape the backslashes again to prevent a string literal from consuming them: "..\\\\&..".

You may want to use the block form to avoid a lot of backslashes.

spickermann
  • 100,941
  • 9
  • 101
  • 131