Javascript 未终止的字符串文字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5296402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:32:25  来源:igfitidea点击:

unterminated string literal

javascript

提问by VIKASH

The following code:

以下代码:

var str= "<strong>English Comprehension<\/strong>
                    <br\/>
                    <ul>
                    <li>  Synonyms/Antonyms/Word Meaning (Vocabulary)<\/li>
                    <li>  Complete the Sentence (Grammar)<\/li>
                    <li>  Spot error/Correct sentence (Grammar/sentence construction)<\/li>
                    <li>  Sentence Ordering (Comprehension skills)<\/li>
                    <li>  Questions based on passage (Comprehension skills)<\/li>
                    <\/ul>
                    <br\/>";

Gives the error: "unterminated string literal". Whats the problem?

给出错误:“未终止的字符串文字”。有什么问题?

回答by Gabriel Hurley

You can't split a string across lines like that in javascript. You could accomplish the same readability by making each line a separate string and concatenating them with the plus sign like so:

您不能像在 javascript 中那样跨行拆分字符串。您可以通过使每一行成为一个单独的字符串并将它们与加号连接来实现相同的可读性,如下所示:

var str = "<strong>English Comprehension</strong>"
    + "<br />"
    + "<ul>"
    + "<li>Synonyms/Antonyms/Word Meaning (Vocabulary)</li>"

and so on...

等等...

回答by eeq

If using multiline string in JavaScript you must use '\' in the end of each line:

如果在 JavaScript 中使用多行字符串,则必须在每行末尾使用 '\':

var str = "abc\
def\
ghi\
jkl";

Also beware extra whitespace if your code is indented.

如果您的代码缩进,还要注意额外的空格。

回答by Shadow Wizard is Ear For You

I suggest to do it differently... have hidden element with that HTML e.g.

我建议以不同的方式来做...有隐藏的元素与该 HTML 例如

<div id="myHiddenDiv" style="display: none;"><strong>English Comprehension</strong>
<br />
...
</div>

Then simply read its inner HTML:

然后简单地阅读它的内部 HTML:

var str = document.getElementById("myHiddenDiv").innerHTML;

The big advantage is that you won't have to fight your way with literal strings and it's much easier to edit, the downside is that you add another element to the DOM. Your choice. :)

最大的优点是您不必为文字字符串而烦恼,而且编辑起来要容易得多,缺点是您向 DOM 添加了另一个元素。你的选择。:)

回答by Don

It is not a best practice to write multiline strings in Javascript.

在 Javascript 中编写多行字符串不是最佳实践。

But, you can do that with the \terminator:

但是,您可以使用\终止符来做到这一点:

var str= "<strong>English Comprehension<\/strong>\
                <br\/>\
                <ul>\
                <li>  Synonyms/Antonyms/Word Meaning (Vocabulary)<\/li>\
                <li>  Complete the Sentence (Grammar)<\/li>\
                <li>  Spot error/Correct sentence (Grammar/sentence construction)<\/li>\
                <li>  Sentence Ordering (Comprehension skills)<\/li>\
                <li>  Questions based on passage (Comprehension skills)<\/li>\
                <\/ul>\
                <br\/>";

Note that trailing spaces are part of the string, so it is better to get rid of them, unless they are intentional:

请注意,尾随空格是字符串的一部分,因此最好去掉它们,除非它们是故意的:

var str= "<strong>English Comprehension<\/strong>\
<br\/>\
<ul>\
<li>  Synonyms/Antonyms/Word Meaning (Vocabulary)<\/li>\
<li>  Complete the Sentence (Grammar)<\/li>\
<li>  Spot error/Correct sentence (Grammar/sentence construction)<\/li>\
<li>  Sentence Ordering (Comprehension skills)<\/li>\
<li>  Questions based on passage (Comprehension skills)<\/li>\
<\/ul>\
<br\/>";

回答by Andrei

A good way to concatenate strings in javascript is fallowing:

在 javascript 中连接字符串的一个好方法是休耕:

var stringBuilder = [];

stringBuilder.push("<strong>English Comprehension</strong>");
stringBuilder.push("<br />");
stringBuilder.push("<ul>");
...
var resultString = stringBuilder.join("");

This method faster than

这种方法比

var str = "a"+"b" +"c"

回答by T.J. Crowder

JavaScript doesn't allow literal line breaks in strings unless you escape them with \:

JavaScript 不允许在字符串中使用文字换行符,除非您使用\以下命令对其进行转义:

var str= "<strong>English Comprehension<\/strong>\
    <br\/>\
    <ul>\
    <li>  Synonyms/Antonyms/Word Meaning (Vocabulary)<\/li>\
    <li>  Complete the Sentence (Grammar)<\/li>\
    <li>  Spot error/Correct sentence (Grammar/sentence construction)<\/li>\
    <li>  Sentence Ordering (Comprehension skills)<\/li>\
    <li>  Questions based on passage (Comprehension skills)<\/li>\
    <\/ul>\
    <br\/>";

You might look at ES2015+ template literalsinstead, which use backticks instead of 'or "and allow literal line breaks:

您可以改为查看ES2015+ 模板文字,它使用反引号代替'or"并允许文字换行:

var str= `<strong>English Comprehension<\/strong>
    <br\/>
    <ul>
    <li>  Synonyms/Antonyms/Word Meaning (Vocabulary)<\/li>
    <li>  Complete the Sentence (Grammar)<\/li>
    <li>  Spot error/Correct sentence (Grammar/sentence construction)<\/li>
    <li>  Sentence Ordering (Comprehension skills)<\/li>
    <li>  Questions based on passage (Comprehension skills)<\/li>
    <\/ul>
    <br\/>`;

But of course, that only works on ES2015-compatible JavaScript engines (or if you transpile).

但当然,这仅适用于兼容 ES2015 的 JavaScript 引擎(或者如果您进行转译)。

Note that within a template literal ${...}has special meaning (allowing you to substitute the result of any expression:

请注意,在模板文字中${...}具有特殊含义(允许您替换任何表达式的结果:

let v = "Ma";
console.log(`Look ${v}, no hands!`); // Look Ma, no hands!

回答by jagc

If you want to maintain your data in one block of string, try:

如果您想将数据保存在一个字符串块中,请尝试:

var str= "<strong>English Comprehension</strong>\n\
                <br />\n\
                <ul>\n\
                <li>  Synonyms/Antonyms/Word Meaning (Vocabulary)</li>\n\
                <li>  Complete the Sentence (Grammar)</li>\n\
                <li>  Spot error/Correct sentence (Grammar/sentence construction)</li>\n\
                <li>  Sentence Ordering (Comprehension skills)</li>\n\
                <li>  Questions based on passage (Comprehension skills)</li>\n\
                </ul>\n\
                <br />";

notice the \n\at the end of each line.

注意\n\每行末尾的 。

回答by DhruvPathak

Remove the newlines, it will work. GOTCHA to keep in mind ;) See it simple works all fine here without newlines: http://jsfiddle.net/87dYh/

删除换行符,它将起作用。要记住的 GOTCHA ;) 在没有换行符的情况下,这里很简单,一切正常:http: //jsfiddle.net/87dYh/

var str= "<strong>English Comprehension<\/strong><br\/><ul>  <li>  Synonyms/Antonyms/Word Meaning (Vocabulary)<\/li>                  <li>  Complete the Sentence (Grammar)<\/li>                   <li>  Spot error/Correct sentence (Grammar/sentence construction)<\/li>     <li>  Sentence Ordering (Comprehension skills)<\/li>                  <li>  Questions based on passage (Comprehension skills)<\/li>                 <\/ul>              <br\/>";

alert(str);

回答by Frankenmint

My code was a response.write() where I needed to insert html to be outputted POST SUbmit on a form within an Iframe. When copying my code over from an text editor the carraige return carried over into the code window. Once I deleted my returns and whitespace between tags so that the code looked like a block paragraph that fixed the issue:

我的代码是一个 response.write(),我需要在其中插入要在 Iframe 中的表单上输出 POST SUbmit 的 html。当从文本编辑器复制我的代码时,carraige 返回继续进入代码窗口。一旦我删除了标签之间的返回和空格,代码看起来就像一个解决问题的块段落:

("loremIPSUM.<br><br><h2>Title</h2><br>loremIPSUM:<br><br><br><a href='http://url1'>lnk1</a><br><br><a href='http://url2'>loremIPSUMlnk2</a><br><br><a href='http://url3'>loremIPSUM</a><br><br><a href='url4'>IF THE case id is: "</a>"+value)