16

I have a string that looks something like this:

"Line 1\nLine 2"

When I call length on it, though, it's one character short:

"Line 1\nLine 2".length // 13

Looking a little closer:

"Line 1\nLine 2".charAt(6)

I find that the \n is being replaced by a single character, which looks like:

"
"

Is there a way to escape that new line into a \n?

H.B.
  • 166,899
  • 29
  • 327
  • 400
nullnullnull
  • 8,039
  • 12
  • 55
  • 107

4 Answers4

23

Whenever you get Javascript to interpret this string, the '\n' will be rendered as a newline (which is a single character, hence the length.)

To use the string as a literal backslash-n, try escaping the backslash with another one. Like so:

"Line 1\\nLine 2"

If you can't do this when the string is created, you can turn the one string into the other with this:

"Line 1\nLine 2".replace(/\n/, "\\n");

If you might have multiple occurrences of the newline, you can get them all at once by making the regex global, like this:

"Line 1\nLine 2\nLine 3".replace(/\n/g, "\\n");
Jon Carter
  • 2,836
  • 1
  • 20
  • 26
  • Does not work if the OS uses different newline character. – Jencel Feb 03 '16 at 13:53
  • That's not what I observe. Could you provide steps to reproduce any failure you may have seen? The environment in which Javascript runs abstracts away the underlying implementation details of the specific operating system. It's true that the metaphor 'leaks' sometimes, but that does not happen in this case, on both Firefox and Node on Windows and Linux. – Jon Carter Feb 03 '16 at 16:49
  • This function: https://github.com/boris-marinov/monad-transformers/blob/master/tutorial/p3.js#L153 – Jencel Mar 03 '16 at 15:17
  • 1
    What if the original string contained both \n and \\n? Your replace() will make them indistinguishable. – Arik Sep 13 '21 at 15:23
  • @Arik No, it doesn't. – KHAYRI R.R. WOULFE Nov 15 '22 at 05:44
6

\n is the newline character. You can use "Line 1\\nLine 2" to escape it.

Keep in mind that the actual representation of a new line depends on the system and could be one or two characters long: \r\n, \n, or \r

kapex
  • 28,903
  • 6
  • 107
  • 121
  • I don't think `\r` is used as a newline in any operating system, it is a carriage return character and not a newline or linefeed character. Windows uses `\r\n` and Unix based systems use `\n` only. – Ɗααɳιടԋ Sყҽԃ Jan 25 '22 at 16:19
  • @ƊααɳιടԋSყҽԃ According to https://en.wikipedia.org/wiki/Newline#Representation `\r` was used in a few old operating systems. But I guess for all practical purposed it can be assumed that only `\r\n` or `\n` exist nowadays. – kapex Jan 25 '22 at 16:31
  • Wow! I never new that a carriage return can be used as a newline character. Thanks for the information though. – Ɗααɳιടԋ Sყҽԃ Jan 25 '22 at 17:08
  • For newlines in strings generated by JavaScript, it's just `\n`. – KHAYRI R.R. WOULFE Nov 15 '22 at 05:45
0

In JavaScript, a backslash in a string literal is the start of an escape code, for instance backslash n for a newline. But what if you want an actual backslash in the resulting string? One of the escape codes is backslash backslash, for an actual backslash.

rakslice
  • 8,742
  • 4
  • 53
  • 57
-1

If anyone else like me, ended up here to unescape new line characters rather than escaping them, this was my solution.

'Line1\\n\\nLine2'.replace(/\\n/g, '\n')

And as utils function

const unescapeNewLine = (str) => str.replace(/\\n/g, '\n')
Jimmi Jønsson
  • 129
  • 1
  • 4