JavaScript view newlines & tabs in string

View a string's newlines and tabs in JavaScript. This function replaces line endings/newlines and tabs:

function raw(str) {
    return str.replace(/\t/g, '\\t')
             .replace(/\r\n/g, '\\r\\n')
             .replace(/\r/g, '\\r')
             .replace(/\n/g, '\\n');

The /g modifier finds and replaces all occurrences.

If you liked this posting, please feel free to leave me a comment below.
View this page on GitHub.
Posted .


Leave a Reply