If you don't want to write out every single replacement, one ugly possibility would be to use
eval to interpret the
\ followed by the escaped character as a string:
const input = String.raw`foo\nbar\tbaz`; console.log(input.replace(/\\(.)/g, (_, char) => eval('"\\' + char + '"')));
CertainPerformance posted this
Have an answer?