未终止的字符串文字

时间:2011-03-14 08:44:28

标签: 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\/>";

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

9 个答案:

答案 0 :(得分:38)

你不能在javascript中的字符串之间拆分字符串。你可以通过使每一行成为一个单独的字符串并用加号连接它们来实现相同的可读性:

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

依旧......

答案 1 :(得分:18)

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

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

如果你的代码是缩进的,也要注意额外的空格。

答案 2 :(得分:11)

我建议采取不同的方式......使用该HTML隐藏元素,例如

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

然后只需阅读其内部HTML:

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

最大的好处是你不必用文字字符串来对抗你的方式而且编辑起来容易得多,缺点是你要为DOM添加另一个元素。你的选择。 :)

答案 3 :(得分:6)

在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\/>";

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

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\/>";

答案 4 :(得分:3)

在javascript中连接字符串的好方法是:

var stringBuilder = [];

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

此方法比

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

答案 5 :(得分:1)

删除换行符,它会起作用。 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);

答案 6 :(得分:1)

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

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

注意每行末尾的\n\

答案 7 :(得分:1)

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\/>";

您可能会改为ES2015+ template literals,它使用反引号代替'"并允许文字换行符:

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\/>`;

但是,当然,这只适用于与ES2015兼容的JavaScript引擎(或者如果你转换的​​话)。

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

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

答案 8 :(得分:0)

我的代码是response.write(),我需要在iframe中的表单上插入html输出POST SUbmit。从文本编辑器复制我的代码时,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)