Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a snippet to have multi lines string in javascript.
var multilineString = (function(){ /*
this
is
a
multiline
string.
*/}).toString().split('\n').slice(1, -1).join('\n');
console.log(multilineString);
@getify

This comment has been minimized.

Copy link

commented Aug 17, 2013

I think there is confusion as to what "multiline string" means. What you've presented here is a string that has multiple lines in it (via new-line characters). That might be useful to some people, and might be annoying to others.

A "multiline string" from the source code perspective usually begets a string with only one "line" of text in it, but that is split up over multiple lines of your source code. That is a different animal altogether than you've presented.

All JS engines support both flavors of strings (without hacks). Example:

var my_string_with_multiple_lines = "this\nis\n\a\nmultiline\nstring.";
console.log(my_string_with_multiple_lines);
/*
this
is
a
multiline
string.
*/
var my_multiline_string = "\
this \
is \
a \
multiline \
string.\
";
console.log(my_multiline_string);
// this is a multiline string.
var my_multiline_string_with_multiple_lines = "\
this\n\
is\n\
a\n\
multiline\n\
string.\
";
console.log(my_multiline_string_with_multiple_lines);
/*
this
is
a
multiline
string.
*/
@jeromeetienne

This comment has been minimized.

Copy link
Owner Author

commented Aug 17, 2013

in three.js the following is usually used. and this is the one im trying to modify.

var multilineString = [
    'this',
    'is',
    'a mutiline',
    'string.',
].join('\n')
console.log(multilineString)

As the options you suggested, it add extra characters in the multiline string. It makes it harder to edit when you work on them. The first option i presented seem to fix this requirement.

@jeromeetienne

This comment has been minimized.

Copy link
Owner Author

commented Aug 19, 2013

hmm it has issue with javascript minifier unfortunatly... they will likely remove this comment to reduce the file size. mrdoob/three.js#3768 (comment)

i tried with @preserve. it is a jsdoc tag which preserve the comment. it is used to preserve comment which contains license text. nevertheless i tried and failed.

this significantly reduce the potential of the snippets :(

@andergmartins

This comment has been minimized.

Copy link

commented Mar 20, 2014

At least this was useful to me in some way :) I didn't know that using the method toString from a function will return its internal comments. Interesting...

@mzbyszewska

This comment has been minimized.

Copy link

commented Sep 20, 2017

Thanks a lot for this snippet. It allows me to deal with unknown multi lines texts.

@ricardo85x

This comment has been minimized.

Copy link

commented Jun 29, 2018

It does not work if inside the comment has the <script></script> tag

@henderbj

This comment has been minimized.

Copy link

commented Aug 19, 2018

For me it worked good. I want it to have PGN chess games into a javascript variable. For sure having html tags, javascript, or php inside the string will cause problems, at least that you add the logic to escape them. For everything rest, this is good. Thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.