irb(main):001:0>"abc".sub(/(b)/,'#\1#')# works=>"a#b#c"irb(main):002:0>"abc".sub(/(b)/,'\\1')# doesn't works, should be "a\bc"=>"abc"irb(main):003:0>"abc".sub(/(b)/,'\\\1')# doesn't works, should be "a\\bc"=>"a\\1c"irb(main):004:0>"abc".sub(/(b)/,"\\1")# works=>"abc"irb(main):005:0>"abc".sub(/(b)/,"\\\\1")# doesn't works, should be "a\bc"=>"a\\1c"irb(main):006:0>"abc".sub(/(b)/,'\ \1')# works=>"a\ bc"
If replacement is a String it will be substituted for the matched text. It may contain back-references to the pattern's capture groups of the form "\d", where d is a group number, or "\k<n>", where n is a group name. If it is a double-quoted string, both back-references must be preceded by an additional backslash.
it is explained what to do with the blackslashes. and how much do you need.
I don't unterstand, why five backslahes are required in single quotes. My understanding was that characters in single quotes won't get interpolated so two backslashes should be sufficient.