0

I have written a function which its input should have line-feeds in it:

function goh(textt) {
    textt = textt.split('\n');
    for(i=0; i<textt.length; i++) {
        textt[i] = i+'='+textt[i]
    }
    return textt.join('\n');
}

which for example I want to call it in this way:

goh("http://www.youtube.com/watch?v=
http://www.youtube.com/watch?v=
http://www.youtube.com/watch?v=
http://www.youtube.com/watch?v=
http://www.youtube.com/watch?v=
http://www.youtube.com/watch?v=
http://www.youtube.com/watch?v=");

but it gives me this error in the firefox's console. (BTW I have read all the questions asked about this before ...)

SyntaxError: unterminated string literal

How can I fix this problem?

EDIT:

Please everyone listen. I knew what are you talking here. in that case I would add the \n myself xD. I want the script does this for me
I want to pass the RAW Input.

Community
  • 1
  • 1
TechLife
  • 143
  • 8
  • 1
    The error is generated because you cannot break strings with newlines like that. – Pointy Mar 22 '15 at 14:07
  • Take a look at this question to get more information: http://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript – Hunter McMillen Mar 22 '15 at 14:08
  • Why not just pass an array instead, it makes no sense to pass in a string to that function – adeneo Mar 22 '15 at 14:12
  • @adeneo I cant pass arrays... in that case it is not automation .. Im doing sth not? – TechLife Mar 22 '15 at 14:17
  • Why not, if you can add `\n` to the string, you can surely push each string to an array instead, and you're creating an array by splitting in the function anyway. – adeneo Mar 22 '15 at 14:17
  • In regards to your edit, if you receive a string with newlines this isn't an issue, the error message clearly states that you have a **string literal**, or in other words a string you literally wrote in javascript with invalid newlines. – adeneo Mar 22 '15 at 14:22
  • @adeneo no no no! consider I want to pass a raw input. __I dont want to do anything to it and it has `\n` in it.__ – TechLife Mar 22 '15 at 14:23
  • And that's not an issue, strings containing newlines are fine. It's when you ***type literal strings*** with actual CR/LF ***in your javascript*** that you have an issue. – adeneo Mar 22 '15 at 14:24
  • @adeneo what do you mean invalid .. also I tried to copy and paste the text to notepad++ and back to the console which didnt solve the problem .. do you have any idea? – TechLife Mar 22 '15 at 14:25

3 Answers3

1

Just use \n. It acts the same.

goh("http://www.youtube.com/watch?v=\nhttp://www.youtube.com/watch?v="); // many more

From your comment

no no no I cant! I want the script do this for me, I just want to pass the raw input ..... not a good answer.

You must use an array and push the "raw input" and join them.

var arr = [];
arr.push("raw data");
arr.push("raw data");
for(var i = 0; i < arr.length; i++)
    console.log(arr[i], "There's no need of split");
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

Regarding :

SyntaxError: unterminated string literal How can I fix this problem?

you should use backslash :

goh("http://www.youtube.com/watch?v=\
http://www.youtube.com/watch?v=\
http://www.youtube.com/watch?v=\
http://www.youtube.com/watch?v=\
http://www.youtube.com/watch?v=\
http://www.youtube.com/watch?v=\
http://www.youtube.com/watch?v=");

if you want to send pure linebreaks use \n as @amit answered.

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 1
    Would this not effectively escape the newline characters in the string, which are expected in the OP's function...? – Robert Rossmann Mar 22 '15 at 14:09
  • 1
    This just allow multiline strings, doesn't preserve the newline though `"\ ".length // 0` -.- replace space with newline and run... – Andreas Louv Mar 22 '15 at 14:10
  • @dev-null regarding the error and his question actually about the error , a backslash would solve it – Royi Namir Mar 22 '15 at 14:13
  • @RoyiNamir After your 5th edit you finally manage to answer a part of OPs question, but it doesn't really help hem though, because now OP will sit and wonder why the split doesn't work. – Andreas Louv Mar 22 '15 at 14:18
  • @dev-null Yes I've edited ( you make it sound bad) becuase wanted to answer for his concrete question. – Royi Namir Mar 22 '15 at 14:19
  • @all I cant no no no I cant! I want the script do this for me, I just want to pass the raw input ..... not a good answer. – TechLife Mar 22 '15 at 14:19
  • @TechLife What is the raw input ? with \n or without ? you **can't** pass rawinput with linebreaks without `\\` – Royi Namir Mar 22 '15 at 14:19
  • @RoyiNamir The raw input is in the question (in the function call - please read the question!) so you mean that this cant be done with javascript? is it a limitation? – TechLife Mar 22 '15 at 14:28
  • @TechLife yes. you can't do it with JS. you cannot pass this multiline (not ended with backslash nor \n )statement to a function. – Royi Namir Mar 22 '15 at 14:28
-1

JavaScript doesn't allow multiline strings, you can however use \n which will give you a newline in the string:

var a = "a\nb\nc";

console.log(a); // will give you:
"a
b
c"

If you think one line will be to cluttered, you can always use string concatenation:

var a = "a\n" + 
  "b\n" + 
  "c";

// using String.prototype.concat    
var a = "a\n".concat(
  "b\n",
  "c\n"
);

// Using Array.prototype.join
var a = ["a", "b", "c"].join('\n');

And now my question is: Why don't you send in an array and forget about the hassle with splitting, joining etc?

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • @TechLife This `\n` and a newline is 100% the same, but when writing strings in JS you cannot simply write a newline, you will need to use `\n`. – Andreas Louv Mar 22 '15 at 14:21
  • @TechLife Can you stop with this "not a good answer" thing? your approach of solving things is not a good one also. you cannot use multiline without slash( Im repeating this for the third time here , and still someone downvoted....baaaaa) – Royi Namir Mar 22 '15 at 14:23
  • @all so you mean this is a limitation? yeah? – TechLife Mar 22 '15 at 14:29