diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/errors/unterminated_string_literal')
| -rw-r--r-- | files/zh-cn/web/javascript/reference/errors/unterminated_string_literal/index.html | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/errors/unterminated_string_literal/index.html b/files/zh-cn/web/javascript/reference/errors/unterminated_string_literal/index.html new file mode 100644 index 0000000000..b0ab684413 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/unterminated_string_literal/index.html @@ -0,0 +1,67 @@ +--- +title: 'SyntaxError: unterminated string literal' +slug: Web/JavaScript/Reference/Errors/Unterminated_string_literal +tags: + - Error + - JavaScript + - SyntaxError +translation_of: Web/JavaScript/Reference/Errors/Unterminated_string_literal +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">SyntaxError: unterminated string literal +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}} </p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>某处 js 解析字符串出错。字符串必须使用单引号或双引号来正确的关闭。在 Javascript 中使用单引号的字符和双引号的字符串是没有区别的。字符串用<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Escape_notation">转义字符</a>不是单引号就是双引。为解决这个错误,检查一下:</p> + +<ul> + <li>你字符串的引号是否成对。</li> + <li>你是否正确使用了转义序列</li> + <li>你的字符串是否在多行中解析正常。</li> +</ul> + +<h2 id="示例">示例</h2> + +<h3 id="多行字符串">多行字符串</h3> + +<p>在javascript中你不能够直接使用多行字符串赋值给一个变量。如下:</p> + +<pre class="brush: js example-bad">var longString = "This is a very long string which needs + to wrap across multiple lines because + otherwise my code is unreadable."; +// SyntaxError: unterminated string literal</pre> + +<p>可以使用<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Addition">"+"运算符</a>,反斜杠,或<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">模板字符串</a>来代替多行。“+”运算符的使用如下:</p> + +<pre class="brush: js example-good">var longString = "This is a very long string which needs " + + "to wrap across multiple lines because " + + "otherwise my code is unreadable."; +</pre> + +<p>或者你可以使用“\”在每一行的末尾,以表示该字符串在下一行继续。要确保“\“之后没有没有空格和任何其他的字符,及缩进,否则该“\”将不会起作用。使用方法如下:</p> + +<pre class="brush: js example-good">var longString = "This is a very long string which needs \ +to wrap across multiple lines because \ +otherwise my code is unreadable."; +</pre> + +<p>另一种方式是使用 ES 2015 的环境所支持<a href="/zh-CN/docs/Web/JavaScript/Reference/Template_literals">模板字符串</a>(反引号` `)。</p> + +<pre class="brush: js example-good">var longString = `This is a very long string which needs + to wrap across multiple lines because + otherwise my code is unreadable.`;</pre> + +<h2 id="相关">相关</h2> + +<ul> + <li>{{jsxref("String")}} </li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">模板字符串</a></li> +</ul> |
