I receive some data after a jQuery $.get
and I would like to be able to see all the non visible characters like spaces, tabs, end of line or new line. Is it possible to see this in chrome console? How?
I receive some data after a jQuery $.get
and I would like to be able to see all the non visible characters like spaces, tabs, end of line or new line. Is it possible to see this in chrome console? How?
- Is it coincidental that all of your examples are whitespace, or are those the only characters you care about? – Daniel Kaplan Commented Dec 12, 2023 at 23:23
4 Answers
Reset to default 8One way would be to a manual replace for all possible whitespace characters:
var html = '\n\t';
console.log(html); // displays whitespace
console.log(html.replace(/\n/g,'\\n').replace(/\t/,'\\t')); // displays '\n\t'
Quite tedious, I know.
You can also use JSON.stringify
const data = 'name\tvalue\n'
console.log(JSON.stringify(data))
// "name\tvalue\n"
Try encodeURI
.
var html = '\n\t';
console.log(html); // displays whitespace
encodeURI(html); //displays "%0A%09"
Neither of the other answers worked for me, when dealing with things like the soft hyphen (\00ad
):
console.log('<p>\u00ad</p>')
// <p></p>
It's there but you can't see it.
So here's a snippet that turns everything but the printable ASCII characters to their unicode escape sequences:
'<p>\u00ad</p>'.replace(
/[^\u0020-\u007f]/g,
x => '\\u' + ('0000' + x.charCodeAt(0).toString(16)).slice(-4)
)
// "<p>\u00ad</p>"
NB: this does not escape quotes or backslashes, so don't use it to e.g. generate JSON. Just for debugging. If you need to e.g. output ASCII-only JSON, try the jsesc library.