From: "Wolfgang Nádasi-Donner" Date: 2007-01-22T00:35:06+09:00 Subject: Re: gsub and backslashes John Wright schrieb: > Say I want to replace all occurrences of \ in a string with \\: > > test = "a string with \\ a backslash" > puts test.gsub(/\\/, "\\\\") #no worky > => a string with \ a backslash > > puts test.gsub(/\\/, "\\\\\\") #why does this work? > => a string with \\ a backslash > > Why does the replacement string have to be six backslashes? Hopefully > this is really simple and I'm just being dense... > Ooops irb(main):005:0> puts "a string with \\ a backslash".gsub(/\\/, "\\\\\\\\") a string with \\ a backslash This works as expected, but why it works with '\'*6 I don't understand. Wolfgang Nádasi-Donner