Issue #11132 has been updated by Usaku NAKAMURA.

Backport changed from 2.0.0: WONTFIX, 2.1: REQUIRED, 2.2: REQUIRED to 2.0.0: WONTFIX, 2.1: DONE, 2.2: REQUIRED

ruby_2_1 r50582 merged revision(s) 50509.

----------------------------------------
Bug #11132: String#sub and character sequence \' in replacement string
https://bugs.ruby-lang.org/issues/11132#change-52541

* Author: Thomas Leitner
* Status: Closed
* Priority: Normal
* Assignee: 
* ruby -v: ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
* Backport: 2.0.0: WONTFIX, 2.1: DONE, 2.2: REQUIRED
----------------------------------------
Hi,

I don't know if this is intentional but substitution of `\'` and `` \` `` in the replacement string was rather unexpected for me:

~~~
2.2.2 :001 > "this is a test".sub(/this/, "some text \\'")
 => "some text  is a test is a test"
~~~

I would have expected the following result:

~~~
 => "some text \\' is a test"
~~~

The documentation says nothing about this, just that back-references can be used (i.e. `\\d` or `\\k<n>`).

A work-around is escaping the escape character:

~~~
2.2.2 :001 > "this is a test".sub(/this/, "some text \\\\'")                                                                                          
 => "some text \\' is a test"
~~~

Thanks!



-- 
https://bugs.ruby-lang.org/