Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an alternate spelling of \n in javascript?

Due to a bug in the Tumblr theme editor, any time the sequence \n appears in the source code, it is converted to an actual line break in the source code itself. Therefore, putting the \n sequence into a Javascript string causes the program to crash, as it breaks the string into multiple lines.

I was wondering if there is another way to notate the newline character in JavaScript, which could allow me to work around this issue.

like image 474
14jbella Avatar asked Jul 26 '18 14:07

14jbella


People also ask

Can we use \n in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.

Is New Line \n or n?

The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen.

What is \r in JavaScript string?

The \r metacharacter matches carriage return characters.


1 Answers

Wow, that's an ugly bug.

Yes, you can use \u000a instead (or \u000A). It's the Unicode escape sequence for the same character. (Or worse case: String.fromCharCode(10).)

Gratuitous example:

console.log("\n" === "\u000a");                // true
console.log("\n" === "\u000A");                // true
console.log("\n" === String.fromCharCode(10)); // true
like image 147
T.J. Crowder Avatar answered Oct 04 '22 03:10

T.J. Crowder