javascript escape newlines in string

convert multiline string to single line string javascript
javascript new line not working
javascript replace n with newline
javascript escape characters
javascript multiline string
javascript string
javascript string line break
javascript replace newline with comma

I have to define a variable in a template so my template renderer will replace values in it. I then have to use this variable in some javascript functions I have.

The variable is itself some html and I want to be able to format it so that it looks like HTML and not an unreadable super long one line string...

<script>
var myHTML = "
  <div>
    content
  </div>"
</script>

The only way I can see to get it to work would be to do this (really ugly)

<script>
var myHTML = ""
  +"<div>"
    +"content"
  +"</div>"
</script>

Is there a friendly way to do this?

Any new line characters inserted in the source are part of the template literal. Using normal strings, you would have to use the following syntax in order to get multi-line strings:

var myHTML = '<div>\n    content\n</div>';

console.log(myHTML);

Escape Sequences in Strings - new line, The newline character creates line breaks within the output of a string, be it simply text or JavaScript-generated HTML <a href="javascript:alert(noBreaks)"​>Long String without Line Breaks</a> >>Nesting Quotes And The Escape Character I have to form a JSON string in which a value is having new line character. This has to be escaped and then posted using AJAX call. Can any one suggest a way to escape the string with JavaScript. I

A commonly used and friendly approach to this is to put the template string in its own <script> element with the type attribute set to text/x-something, as this element will be accessible by javascript, but not shown by the browser.

This allows you to write HTML (as well as template markup) as you normally would, without the need for escaping or special handling of newlines.

A working demo of this approach:

//get the template string
var myHTML = document.getElementById("template1").innerHTML;

//check that myHTML contains the template string
console.log(myHTML);
<script type="text/x-template" id="template1">
  <div>
    content
  </div>
</script>

Creating Line Breaks In JavaScript Strings, Escape Character. Because strings must be written within quotes, JavaScript will misunderstand this string: var x = "We are the so-called "Vikings" from the north. In JavaScript, String#charCodeAt () can be used to get the numeric Unicode code point of any character up to U+FFFF (i.e. the character with code point 0xFFFF, which is 65535 in decimal). Since JavaScript uses UCS-2 encoding internally, higher code points are represented by a pair of (lower valued)

  1. You just have to put backward slash "\" at the end of the each line (except last line) of the string and then press enter to go in newline.
  2. In your case:

    <script> 
     var myHTML = '\
          <div>\
            content\
          </div>\
         ';
    </script> 
    

JavaScript Strings, It goes through and removes all types of line breaks from the designated text string. The "gm" at the end of the regex statement signifies that the replacement  The Newline Character To keep long concatenation output readable, JavaScript provides two ways to create line breaks within your string. The first is the newline character ( ). The newline character creates line breaks within the output of a string, be it simply text or JavaScript-generated HTML.

Remove Line Breaks with Javascript, A free online tool to escape or unescape Javascript strings. with \f; Newline is replaced with \n; Carriage return is replaced with \r; Single quote is replaced with​  Escape Character Because strings must be written within quotes, JavaScript will misunderstand this string: The string will be chopped to "We are the so-called ". The solution to avoid this problem, is to use the backslash escape character.

Free Online JavaScript (JS) Escape / Unescape Tool , Multi-line JavaScript strings can be created by adding a backslash at the end Note that the second string includes line breaks within the string itself. When I first found out that you could escape newlines, I thought it was a  JSON String Escape / Unescape. Escapes or unescapes a JSON string removing traces of offending characters that could prevent parsing. The following characters are reserved in JSON and must be properly escaped to be used in strings: Backspace is replaced with \b. Form feed is replaced with \f. Newline is replaced with .

Multi-Line JavaScript Strings, Line breaks in strings vary from platform to platform, but the most common ones are the following: Windows: \r\n carriage return followed by newline character. String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (that is, called without using the new keyword) are primitive strings. JavaScript automatically converts primitives to String objects, so that it's possible to use String object methods for primitive strings.

Comments
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • Thanks. I was searching for javascript string literals and I wasn't coming up with anything
  • why not to put the whole template in a single file "template.html", then you can use an ajax httrequest to put the whole content inside a variable? This could make the whole thing much readable and easy to update.