Project

General

Profile

Actions

Bug #11132

closed

String#sub and character sequence \' in replacement string

Added by gettalong (Thomas Leitner) over 9 years ago. Updated over 9 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
[ruby-core:69121]

Description

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!

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0