Q
I have the following code in Ruby. I want to convert this code into JavaScript. what’s the equivalent code in JS?
text = <<"HERE"
This
Is
A
Multiline
String
HERE
A-1
ECMAScript 6 (ES6) introduces a new type of literal, namely template literals. They have many features, variable interpolation among others, but most importantly for this question, they can be multiline.
ECMAScript 6 (ES6) introduced a new type of text, namely
模板文字
. They have many functions, including variable interpolation, but the most important thing for this problem is that they can be multi-line.
A template literal is delimited by backticks:
Template text is separated by backticks:
var html = `
<div>
<span>Some HTML here</span>
</div>
`;
(Note: I’m not advocating to use HTML in strings)
(Note: I do not advocate using HTML in strings)
A-2
As the first answer mentions, with ES6/Babel, you can now create multi-line strings simply by using backticks:
As mentioned in the first answer, with ES6/Babel, you can now create multi-line strings just by using backticks:
const htmlString = `Say hello to
multi-line
strings!`;
Interpolating variables is a popular new feature that comes with back-tick delimited strings:
Interpolation variables are a popular new feature, with strings separated by backticks:
const htmlString = `${
user.name} liked your post about strings`;
This just transpiles down to concatenation:
This is just converted to concatenation:
user.name + ' liked your post about strings'
Original ES5 answer:
ES5 original answer:
Google’s JavaScript style guide recommends to use string concatenation instead of escaping newlines:
Google的JavaScript样式指南
It is recommended to use string concatenation instead of newline:
Do not do this:
Do not do this:
var myString = 'A rather long string of English text, an error message \
actually that just keeps going and going -- an error \
message to make the Energizer bunny blush (right through \
those Schwarzenegger shades)! Where was I? Oh yes, \
you\'ve got an error and all the extraneous whitespace is \
just gravy. Have a nice day.';
The whitespace at the beginning of each line can’t be safely stripped at compile time; whitespace after the slash will result in tricky errors; and while most script engines support this, it is not part of ECMAScript.
The whitespace at the beginning of each line cannot be safely stripped at compile time; spaces after the slash will cause tricky errors; although most script engines support this feature, it is not part of ECMAScript.
Use string concatenation instead:
Please use string concatenation:
var myString = 'A rather long string of English text, an error message ' +
'actually that just keeps going and going -- an error ' +
'message to make the Energizer bunny blush (right through ' +
'those Schwarzenegger shades)! Where was I? Oh yes, ' +
'you\'ve got an error and all the extraneous whitespace is ' +
'just gravy. Have a nice day.';
A-3
the pattern text = <<"HERE" This Is A Multiline String HERE
is not available in js (I remember using it much in my good old Perl days).
This mode
text = <<"HERE" This Is A Multiline String HERE
is not available in js (I remember I used it a lot during my good time in Perl).
To keep oversight with complex or long multiline strings I sometimes use an array pattern:
In order to supervise complex or long multi-line strings, the array mode is sometimes used:
var myString =
['<div id="someId">',
'some content<br />',
'<a href="#someRef">someRefTxt</a>',
'</div>'
].join('\n');
or the pattern anonymous already showed (escape newline), which can be an ugly block in your code:
Or the anonymous mode (newline character) that has been shown, which may be an ugly block in the code:
var myString =
'<div id="someId"> \
some content<br /> \
<a href="#someRef">someRefTxt</a> \
</div>';
Here’s another weird but working 'trick’1:
This is another weird but effective "trick" 1:
var myString = (function () {
/*
<div id="someId">
some content<br />
<a href="#someRef">someRefTxt</a>
</div>
*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
ES20xx supports spanning strings over multiple lines using template strings:
ES20xx supports the use of
模板字符串
spanning multiple lines字符串
:
let str = `This is a text
with multiple lines.
Escapes are interpreted,
\n is a newline.`;
let str = String.raw`This is a text
with multiple lines.
Escapes are not interpreted,
\n is not a newline.`;
1 Note: this will be lost after minifying/obfuscating your code
1 Note: After minifying/obfuscating the code, this will be lost
A-4
141
I came up with this very jimmy rigged method of a multi lined string. Since converting a function into a string also returns any comments inside the function you can use the comments as your string using a multilined comment /**/. You just have to trim off the ends and you have your string.
I came up with this very clumsy manipulation of multi-line strings. Since converting a function to a string will also return any comments inside the function, you can use multi-line comments / ** / to use comments as a string. You only need to trim the ends.
var myString = function(){
/*
This is some
awesome multi-lined
string using a comment
inside a function
returned as a string.
Enjoy the jimmy rigged code.
*/}.toString().slice(14,-3)
alert(myString)