最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to see the tab eol spaces in chrome console? - Stack Overflow

programmeradmin4浏览0评论

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?

Share Improve this question edited Nov 15, 2016 at 11:13 Nakilon 35.1k16 gold badges111 silver badges146 bronze badges asked Mar 1, 2014 at 1:36 user2040597user2040597 4893 gold badges8 silver badges22 bronze badges 1
  • 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
Add a comment  | 

4 Answers 4

Reset to default 8

One 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.

发布评论

评论列表(0)

  1. 暂无评论