Escape Back Referencing

Ruby has more than one way to access additional information about the most recent regex match, like captured groups. One way is using the special variables $`, $&, $', $1 - $9, $+, and also in the MatchData object $~. They become available after using a method that matched a regex, or when the method supports a block, they are already available in the block.

However, there is also a special string processing supported by the string replacement methods String#gsub and String#sub. The replacement string (second parameter) can contain back references, which behave similarly to their corresponding special variable:

"Idiosyncratic Ruby".sub(/(\w+) (\w+)/, '\2 \1') # => "Ruby Idiosyncratic"

Special Regex Variables & Back References

Perlish Back-Ref Effect Example
$& '\&', '\0'¹ Match "abc".gsub(/.*/, '\&\&') # => "abcabc"
$` '\`' Pre Match "abc".gsub(/b/, '\`') # => "aac"
$' '\\\'' Post Match "abc".gsub(/b/, '\\\'') # => "acc"
$1² '\1' 1st Capture "abc".gsub(/(a)b(c)/, '\1') # => "a"
$+ '\+' Last Capture "abc".gsub(/(a)b(c)/, '\+') # => "c"
$~[:name] '\k<name>' Named Capture "abc".gsub(/(?<name>a)bc/, '\k<name>') # => "a"

Escaping

Ruby is absolutely confusing when it comes to how to escape back references. You have to use one or two backspaces when using single quoted strings. You have to use two (or sometimes three) backspaces when using double quoted strings. Escaping \' needs special attention³:

X '\X' '\\X' '\\\X' "\X" "\\X" "\\\X" "\\\\X"
& Match Match "\\&" "&" Match Match "\\&"
` Pre-Match Pre-Match "\\`" "`" Pre-Match Pre-Match "\\`"
' "'" - Post-Match "'" Post-Match Post-Match "\\'"
Match Match "\\0" "\u0000" Match "\\\u0000" "\\0"
1st Capture 1st Capture "\\1" "\u0001" 1st Capture "\\\u0001" "\\1"
+ Last Capture Last Capture "\\+" "+" Last Capture Last Capture "\\+"
k<name> Named Capture Named Capture "\\k<name>" "k<name>" Named Capture Named Capture "\\k<name>"

¹ Although the global variable $0 is not related to regex matching, \0 is a valid back reference.
² Same for 2-9: Nth Capture
³ If you want to replace ' with \' (backspace quote, no back-ref), the replacement string is: '\\\\\''

More Idiosyncratic Ruby