>>> var input = 'a\nb\nc'; undefined >>> input.replace(/(\r\n)/g, '\\$1'); "a b c" >>> input.replace(/(\r\n)/g, "\\$1"); "a b c" >>> input.replace(/\n/g, '\\n'); "a\nb\nc"