Is there a way to make this string:
foo
bar
Appear like this:
foo\r\n\tbar
It would really help with debugging a lexer.
Is there a way to make this string:
foo
bar
Appear like this:
foo\r\n\tbar
It would really help with debugging a lexer.
The key is to escape \n
in a string replace.
let specialCharacters = [
{regex: /\n/g, replacement: '\\n'},
{regex: /\t/g, replacement: '\\t'}
];
function escapeSpecialCharacters(str){
specialCharacters.forEach(c => {
str = str.replace(c.regex, c.replacement);
});
return str;
}
console.log(escapeSpecialCharacters(`test
test
test
1234`));
If debugging is all you want to do, you can display all escaped characters it in the browser console, but putting the string into an array:
let string = "test\ntest";
let arr = [];
arr.push(string);
console.log(arr);
But that will not change your string in any way, it still contains new line, instead of "\n".