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

javascript - console.log Stripping Carriage Returns (displaying as a literal ↵) - Stack Overflow

programmeradmin3浏览0评论

I'm trying to write a JavaScript function that prints a string to the console via console.log. However, the string has carriage returns in it, which show up as a literal ↵ character instead of creating a new line. Is this a limitation of console.log, or is there a way around this?

Thanks!

Edit: I'm actually trying to print this function inside an object. Something like:

function blah() {
};
console.log({ "function" : blah });

I didn't think to mention it initially, but after trying crowjonah's solution I realize that console.log apparently treats strings passed directly differently from strings passed inside another object.

I'm trying to write a JavaScript function that prints a string to the console via console.log. However, the string has carriage returns in it, which show up as a literal ↵ character instead of creating a new line. Is this a limitation of console.log, or is there a way around this?

Thanks!

Edit: I'm actually trying to print this function inside an object. Something like:

function blah() {
};
console.log({ "function" : blah });

I didn't think to mention it initially, but after trying crowjonah's solution I realize that console.log apparently treats strings passed directly differently from strings passed inside another object.

Share Improve this question edited Apr 16, 2020 at 14:37 ruffin 17.5k10 gold badges96 silver badges149 bronze badges asked Nov 27, 2012 at 17:23 Jake LazaroffJake Lazaroff 911 gold badge3 silver badges8 bronze badges 3
  • 1 What are you using for your carriage return? \r\n seems to work for me in the chrome console and with console.log() – BZink Commented Nov 27, 2012 at 17:25
  • \n, but there's an additional constraint I didn't think to mention; see edited question. – Jake Lazaroff Commented Nov 27, 2012 at 18:15
  • It'd help if we knew what blah() was. – crowjonah Commented Nov 27, 2012 at 18:32
Add a ment  | 

4 Answers 4

Reset to default 6

Chrome will render \n as when printing objects that contain multi-line strings. However, you can simply double-click on the logged string to see it with proper newlines.

use \n in the log message wherever you'd like there to be a line return.

console.log('first line \nsecond line');

if the "carriage returns" are html elements, like <br>, you can run a replace on the string to do it automatically

var newLogMessage = multiLineLogMessage.replace('<br>', '\n');
console.log(newLogMessage);

This is a limitation of console. but you can create a work around:

function multiLineLog(msg) {
    msg = msg.split(/[\r\n]+/g);
    for (var a=0; a < msg.length; a++) console.log(msg[a]);
}

Here is a modified version of SReject's solution:

const log = line => console.log(line);
const multiLineLog = msg => msg.split(/[\r\n]+/g).forEach(log);

const data = { a: { b: { c: null } } };

multiLineLog(JSON.stringify(data, null, 2));
.as-console-wrapper { top: 0; max-height: 100% !important; }

发布评论

评论列表(0)

  1. 暂无评论