考虑以下代码:
var myVar = 'This is my text.\nAnd other information.\rHow to console.log\n\r?';
console.log(myVar);
输出:
This is my text.
And other information.
How to console.log
?
如何使用\n
,\r
& \n\r
?
答案 0 :(得分:13)
我发现自己使用JSON.stringify
来打印这样的内容。
<强>代码:强>
console.log(JSON.stringify("i am a string!\nwith some newlines\r\n!"));
<强>输出:强>
"i am a string!\nwith some newlines\r\n!"
答案 1 :(得分:3)
得到了一些solution:
<强>代码强>:
var myVar = 'This is my text.\nAnd other information.\rHow to console.log\n\r?';
console.log(require('util').inspect(myVar, { showHidden: true, depth: null }));
console.log('----------------');
console.log('Just console.log');
console.log(myVar);
<强>输出强>:
This is my text.\nAnd other information.\rHow to console.log\n\r?
----------------
Just console.log
This is my text.
And other information.
How to console.log
?
其他方式也受欢迎。
答案 2 :(得分:0)
据我所知,如果你在console.log中输出一些特殊的字符
示例:%实际上是控制台日志的控件,你必须将其urlencode为:%25
因为%c执行输出的css格式化,例如
换行符不会显示为
答案 3 :(得分:0)
这是一个古老的问题,但是对于像我一样落入此页面的用户来说,一个更简单的解决方案是用分号“ \”代替转义符。
$ cat crl1.pem > total_crl.pem
$ cat crl2.pem >> total_crl.pem
$ cat crl3.pem >> total_crl.pem