6

Is there any way to force Chrome's JS console to display newlines like Firefox?

Chrome:

enter image description here

Firefox:

enter image description here

Possibly a hidden switch somewhere?

Blender
  • 289,723
  • 53
  • 439
  • 496
  • I suppose something like `x.replace(/\n/g,"\\n")` is out of the question? (Or use FF, which seems to do what you want by default.) – nnnnnn Oct 09 '12 at 04:19
  • @nnnnnn: That's the behavior that I'm trying to emulate in Chrome. There doesn't seem to be a hidden switch anywhere, though. – Blender Oct 09 '12 at 04:24
  • Yeah, I figured. I guess worst-case if you need to copy-paste the value or something you could manually enter a `.replace()` statement as per my previous comment, or define a function to do it so you don't have to type it every time. (Clunky? Yes, I admit it.) – nnnnnn Oct 09 '12 at 04:32
  • can you tell us how to display those characters in firefox? I'm around this issue for 20 minutes and I can't get by googling it a good answer for this problem – João Pimentel Ferreira Jan 29 '18 at 13:51
  • @JoãoPimentelFerreira: Firefox stopped displaying strings like this starting with 23.0. – Blender Jan 29 '18 at 18:38

4 Answers4

9

You can use encodeURI for showing the hidden stuffs.

Something like this encodeURI("a\nb") instead of "a\nb".

Chrome EncodeURI

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
2

In node.js, require("util").inspect does something very similar. I haven't been able to find a browser equivalent, though fortunately the node.js implementation is fairly straight forward:

JSON.stringify(value)
    .replace(/^"|"$/g, '')
    .replace(/'/g, "\\'")
    .replace(/\\"/g, '"')
;

In your case, just JSON.stringify(value) should work.

Hope this helps.

Xavi
  • 20,111
  • 14
  • 72
  • 63
0

You can try this way

var x = 'a\\nb';

EDIT:

You can use hexadecimal character in string.

\ = '\u005C'
> var x = 'a\u005Cnb';
> x
<- "a\nb"
> x === "a\nb" is false.
> x === "a\\nb" is true or x === 'a\u005Cnb' is true.

You can take a look at links.

http://mathiasbynens.be/notes/javascript-escapes http://code.cside.com/3rdpage/us/javaUnicode/converter.html

Sinan AKYAZICI
  • 3,942
  • 5
  • 35
  • 60
  • But `x !== 'a\nb'`. I'm just trying to change how the console *displays* `x`, not `x` itself. Firefox's built-in console displays control characters like `\n` as *literally* `\n`. Chrome renders them into actual newlines. – Blender Oct 09 '12 at 04:23
  • I've edited my question and added screenshots. That should clear up the confusion. – Blender Oct 09 '12 at 04:57
0

You can stringify the value to get these invisible characters:

> JSON.stringify("a\nb")
<- ""a\nb""
Thomas Hickman
  • 145
  • 1
  • 12