[ruby-core:123560] [Ruby Bug#21651] replacing a string with one backslash with two backslashes
Issue #21651 has been reported by tdrive (Stanislav Boldaev). ---------------------------------------- Bug #21651: replacing a string with one backslash with two backslashes https://bugs.ruby-lang.org/issues/21651 * Author: tdrive (Stanislav Boldaev) * Status: Open * ruby -v: 3.4.4 * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- ruby 2.7.8p225 (2023-03-30 revision 1f4d455848) [x86_64-linux] ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-linux] ``` irb(main):002> "\\".gsub("\\", "\\\\") => "\\" irb(main):003> "\\".gsub("\\", "\\ \\") => "\\ \\" ``` The replacement is happening, as you can see from the version with the space, but why is the result so strange without the space? Similar code works fine in JS: ``` "\\".replace("\\", "\\\\")
'\\\\'
How to replace one backslash with two backslashes?
--
https://bugs.ruby-lang.org/
Issue #21651 has been updated by nobu (Nobuyoshi Nakada). Status changed from Open to Feedback From [String@Substitution+Methods]:
Note that <tt>\\\\</tt> is interpreted as an escape, i.e., a single backslash.
Note also that a string literal consumes backslashes. See [String Literals] for details about string literals.
This is what happens at the first line. Since a space after a backslash is not a valid escape, the result at the second line is `"\ \"`.
If you want to write a non-back-reference string <tt>\&</tt> in `replacement`, you need to first 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: <tt>"..\\\\\\\\&.."</tt>.
For your example: ```ruby "\\".gsub("\\", "\\\\\\\\") # literally two backslashes "\\".gsub("\\", "\\&\\&") # double the matched substrings "\\".gsub("\\") {"\\\\"} # the block result used literally ``` [String@Substitution+Methods]: https://docs.ruby-lang.org/en/master/String.html#class-String-label-Substitu... [String Literals]: https://docs.ruby-lang.org/en/master/syntax/literals_rdoc.html#label-String+... ---------------------------------------- Bug #21651: replacing a string with one backslash with two backslashes https://bugs.ruby-lang.org/issues/21651#change-114945 * Author: tdrive (Stanislav Boldaev) * Status: Feedback * ruby -v: 3.4.4 * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- ruby 2.7.8p225 (2023-03-30 revision 1f4d455848) [x86_64-linux] ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-linux] ``` irb(main):002> "\\".gsub("\\", "\\\\") => "\\" irb(main):003> "\\".gsub("\\", "\\ \\") => "\\ \\" ``` The replacement is happening, as you can see from the version with the space, but why is the result so strange without the space? Similar code works fine in JS: ``` "\\".replace("\\", "\\\\")
'\\\\'
How to replace one backslash with two backslashes?
--
https://bugs.ruby-lang.org/
participants (2)
-
nobu (Nobuyoshi Nakada) -
tdrive (Stanislav Boldaev)