I want to replace and insert escape character sequences with their escape character values, also taking into account that \'\\\' nullifies the escape character.
For exam
I assume you are reffering to the following problem:
"\\n".gsub(/\\\\/, "\\").gsub(/\\n/, "\n") # => "n"
"\\n".gsub(/\\n/, "\n").gsub(/\\\\/, "\\") # => "\\\n"
String#gsub
can take a block argument, which performs the substitution.
str.gsub(/\\(.)/) do |s|
case $1
when "n"
"\n"
when "t"
"\t"
else
$1
end
end
This way no special escape sequence is substituted first and everything works as expeted.
You want the opposite of this?
puts "This is a \n test. Here is a \\n which represents a newline"
=>
This is a test. Here is a \n which represents a newline