Project

General

Profile

Bug #12703

String.gsub returns unexpected results with numeric match characters

Added by nlaveck (Nathan LaVeck) about 3 years ago. Updated about 3 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.4.0dev (2016-08-25 trunk 56006)
[ruby-core:77051]

Description

I'm unable to prepend a backslash to numeric group matches using gsub

irb(main):053:0> "z'z".gsub(/(')/, '\\1')
=> "z'z"

Expected Result

=> "z\'z"

Adding an extra backslash gives me this:

irb(main):056:0> "z'z".gsub(/(')/, '\\\1')
=> "z\\1z"

Tested on Ruby version 2.4.0

History

Updated by nlaveck (Nathan LaVeck) about 3 years ago

  • ruby -v set to ruby 2.4.0dev (2016-08-25 trunk 56006)

Updated by nobu (Nobuyoshi Nakada) about 3 years ago

  • Status changed from Open to Rejected

What you want is "z'z".gsub(/(')/, '\\\\\1').
You need to escape twice, for a literal, and for gsub.

Also available in: Atom PDF