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
Adding Newline Characters in a String In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.
In HTML, the <br> element creates a line break. You can add it wherever you want text to end on the current line and resume on the next.
There are two ways to break JavaScript code into several lines: We can use the newline escape character i.e “\n”. if we are working on the js file or not rendering the code to the html page. We can use the <br> tag.
The \r metacharacter matches carriage return characters.
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 XXXXEscape \n
with \\n
and store the string in DB. However, only \n
can also be stored in DB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With