函数输入上未终止的文字字符串

Unterminated Literal String on function input

我写了一个函数,它的输入应该有换行符:

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

例如我想这样称呼它:

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=");

但是它在 firefox 的控制台中给我这个错误。 (顺便说一句,我之前已经阅读了所有关于此的问题...)

SyntaxError: unterminated string literal

我该如何解决这个问题?

编辑:

请大家听。我知道你在这里说什么。在那种情况下,我会自己添加 \n xD。 我希望脚本为我执行此操作
我想通过 RAW 输入。

关于 :

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

你应该使用反斜杠:

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=");

如果您想发送纯换行符,请使用 \n 作为@amit 的回答。

只需使用\n。它的作用是一样的。

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

来自您的评论

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.

您必须使用数组并推送 "raw input" 并加入它们。

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");

JavaScript 不允许多行字符串,但是您可以使用 \n 它将在字符串中给您一个换行符:

var a = "a\nb\nc";

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

如果您认为一行太杂乱,您可以随时使用字符串连接:

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');

现在我的问题是:为什么不发送一个数组而忘记拆分、合并等的麻烦?