9

Example:

var i = 'Hello \n World'
console.log(i)

Would return:

Hello
World

and I want it to return

Hello \n World

not rendering the new line, as I intend to store this in a database.


FOR THOSE WHO WANT TO STORE \n in Database

You don't need to escape, as your Document Database will do JSON.stringify, I use ArangoDB and it works perfectly fine, thanks to @PaulPro

Rifky Niyas
  • 1,737
  • 10
  • 25
itsezc
  • 692
  • 2
  • 7
  • 20
  • 4
    You have to escape the escape character `\\n` –  Feb 08 '18 at 18:07
  • You can store newline characters in databases... There must be some other reason you want to store it as two characters instead of one. – Paul Feb 08 '18 at 18:08
  • https://stackoverflow.com/questions/14138706/how-should-i-store-newlines-in-a-mysql-database/14138880#14138880 – Vicky charanpahari Feb 08 '18 at 18:10
  • @ProfessorAllman how would I do this, is there a built-in function in javascript to do this? – itsezc Feb 08 '18 at 18:10
  • @Paulpro this is because \n creates news lines and can't be stored in JSON or Document database – itsezc Feb 08 '18 at 18:11
  • 2
    @Lieblingsmensch When you store something in a DB you should escape it properly for the DB as you insert. It is a very bad practice to try escaping yourself in advance and can lead to major security vulnerabilities. If you are inserting JSON your DB layer should JSON.stringify its input. `JSON.stringify( 'Hello \n World' )` also gives you the correct value to store in the DB, but `JSON.stringify( 'Hello \\n World' )` will give you the wrong value because it will be double escaped then. – Paul Feb 08 '18 at 18:23
  • @Paulpro Thanks for the heads up, I'll keep that in mind – itsezc Feb 08 '18 at 18:25
  • 1
    Okay after editing my code, @PaulPro is absolutely spot on, no need to escape characters as the Document Database will most likely do it for you, if not you still only have to do \n and not \\n – itsezc Feb 08 '18 at 20:49

3 Answers3

17

You would escape the \ with \\, which would tell the interpreter just to produce the character without processing it as a special character:

var i = 'Hello \\n World';
console.log(i)

Here are all the string escape codes:

  • \0 The NUL character (\u0000)
  • \b Backspace (\u0008)
  • \t Horizontal tab (\u0009)
  • \n Newline (\u000A)
  • \v Vertical tab (\u000B)
  • \f Form feed (\u000C)
  • \r Carriage return (\u000D)
  • \" Double quote (\u0022)
  • \' Apostrophe or single quote (\u0027)
  • \\ Backslash (\u005C)
  • \x XX The Latin-1 character specified by the two hexadecimal digits XX
  • \u XXXX The Unicode character specified by the four hexadecimal digits XXXX
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
2

Escape \n with \\n and store the string in DB. However, only \n can also be stored in DB.

CodeManX
  • 11,159
  • 5
  • 49
  • 70
1

Use "\\n" instead of "\n" for this to work, same case with selenium java

      WebElement searchBox = driver.findElement(By.name("q"));
      searchBox.sendKeys("Test \\n");
      searchBox.submit();
v_vista
  • 11
  • 3