Project

General

Profile

Actions

Bug #12703

closed

String.gsub returns unexpected results with numeric match characters

Added by nlaveck (Nathan LaVeck) over 7 years ago. Updated over 7 years ago.

Status:
Rejected
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

Updated by nlaveck (Nathan LaVeck) over 7 years ago

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

Updated by nobu (Nobuyoshi Nakada) over 7 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0