如何在 JavaScript 中将字符串分解为多行代码?
JavaScript 中是否有一个字符可以分解一行代码,以便即使在新行上也可以将其读取为连续的?
就像是....
1. alert ( "Please Select file 2. \ to delete" );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
在您的示例中,您可以将字符串分成两部分:
或者,当它是一个字符串时,就像您的情况一样,您可以使用 反斜杠 正如 @Gumbo 建议的:
请注意,这种反斜杠方法是 不一定是首选,并且可能没有得到普遍支持(我很难找到这方面的硬数据)。 ECMA 5.1 中不规格。
当使用其他代码(不在引号中)时,换行符将被忽略,并且完全可以接受。 例如:
In your example, you can break the string into two pieces:
Or, when it's a string, as in your case, you can use a backslash as @Gumbo suggested:
Note that this backslash approach is not necessarily preferred, and possibly not universally supported (I had trouble finding hard data on this). It is not in the ECMA 5.1 spec.
When working with other code (not in quotes), line breaks are ignored, and perfectly acceptable. For example:
将反斜杠放在行尾:
编辑 我必须注意,这不是 ECMAScript 字符串 为 行终止字符 根本不允许:
所以使用字符串连接是更好的选择。
更新 2015-01-05 ECMAScript5 中的字符串文字 允许上述语法:
Put the backslash at the end of the line:
Edit I have to note that this is not part of ECMAScript strings as line terminating characters are not allowed at all:
So using string concatenation is the better choice.
Update 2015-01-05 String literals in ECMAScript5 allow the mentioned syntax:
ECMAScript 6 引入了模板字符串:
例如:
会提醒:
ECMAScript 6 introduced template strings:
For example:
will alert:
将绳子分成两段
Break up the string into two pieces
值得注意的是。 尝试过:
这有效。 然而,意外的是,最后一个反斜杠后面有一个空格字符(所有其他反斜杠都在行尾)。 这导致了 javascript 的错误! 不过,删除这个空格可以修复错误。
这是使用 Cordova 的 Android ADT。
Interesting to note. Tried:
And this worked. However, on accident!, there was a space character following the final backslash (all other backslashes were at the end of the line). And this caused an error in the javascript! Removing this space fixed the error, though.
This is in ADT for Android using Cordova.
你可以使用
那应该可以
You can just use
That should work
您可以将长字符串常量分解为逻辑块并将它们分配到数组中。 然后使用空字符串作为分隔符进行
join
。输出将是:
这种方式会稍微影响性能,但会提高代码的可读性和可维护性。
You can break a long string constant into logical chunks and assign them into an array. Then do a
join
with an empty string as a delimiter.Output will be:
There's a slight performance hit this way but you gain in code readability and maintainability.
反斜杠运算符不可靠。 尝试将此函数粘贴到浏览器控制台中:
然后运行它。 由于函数内的常规(且正确)缩进,将包含两个额外的空格,从而导致
someLongLineOfText ThatShouldNotBeBroken
。在这种情况下,即使使用反引号也无济于事。 始终使用串联“+”运算符来防止此类问题。
The backslash operator is not reliable. Try pasting this function in your browser console:
and then run it. Because of the conventional (and correct) indentation within the function, two extra spaces will be included, resulting in
someLongLineOfText ThatShouldNotBeBroken
.Even using backticks will not help in this case. Always use the concatenation "+" operator to prevent this type of issue.
如果字符串分解为多行导致问题,这里是 VSCode 用户的一个很好的解决方案 (当我必须测试长 JWT 令牌时,我遇到了这个问题,并且不知何故使用模板文字并没有解决问题。)
A good solution here for VSCode users, if a string breaking down into multiple lines causes the problem (I faced this when I had to test a long JWT token, and somehow using template literals didn't do the trick.)
我尝试了上述一些建议,但在 Chrome 代码检查器中收到了非法字符警告。 以下内容对我有用(尽管只在 Chrome 中进行了测试!)
结果就像...
注意双反斜杠!!...这似乎很重要!
I tried a number of the above suggestions but got an ILLEGAL character warning in Chrome code inspector. The following worked for me (only tested in Chrome though!)
comes out like...
NOTE the double backslash!!...this seems to be important!
无需手动中断代码。
只需在要中断的地方添加 \n 即可。
这将显示如下警报
No need of any manual break in code.
Just add \n where you want to break.
This will show the alert like