如何在 JavaScript 中将字符串分解为多行代码?

发布于 2024-07-13 07:20:04 字数 139 浏览 6 评论 0 原文

JavaScript 中是否有一个字符可以分解一行代码,以便即使在新行上也可以将其读取为连续的?

就像是....

1. alert ( "Please Select file   
2. \ to delete" );

Is there a character in JavaScript to break up a line of code so that it is read as continuous despite being on a new line?

Something like....

1. alert ( "Please Select file   
2. \ to delete" );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

帝王念 2024-07-20 07:20:04

在您的示例中,您可以将字符串分成两部分:

alert ( "Please Select file"
 + " to delete");

或者,当它是一个字符串时,就像您的情况一样,您可以使用 反斜杠 正如 @Gumbo 建议的:

alert ( "Please Select file\
 to delete");

请注意,这种反斜杠方法是 不一定是首选,并且可能没有得到普遍支持(我很难找到这方面的硬数据)。 ECMA 5.1 中规格

当使用其他代码(不在引号中)时,换行符将被忽略,并且完全可以接受。 例如:

if(SuperLongConditionWhyIsThisSoLong
  && SuperLongConditionOnAnotherLine
  && SuperLongConditionOnThirdLineSheesh)
{
    // launch_missiles();
}

In your example, you can break the string into two pieces:

alert ( "Please Select file"
 + " to delete");

Or, when it's a string, as in your case, you can use a backslash as @Gumbo suggested:

alert ( "Please Select file\
 to delete");

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:

if(SuperLongConditionWhyIsThisSoLong
  && SuperLongConditionOnAnotherLine
  && SuperLongConditionOnThirdLineSheesh)
{
    // launch_missiles();
}
抚笙 2024-07-20 07:20:04

将反斜杠放在行尾:

alert("Please Select file\
 to delete");

编辑    我必须注意,这不是 ECMAScript 字符串行终止字符 根本不允许:

LineTerminator”字符不能出现在字符串文字中,即使前面有反斜杠 \。 使行终止符成为字符串文字的字符串值一部分的正确方法是使用转义序列,例如 \n\u000A

所以使用字符串连接是更好的选择。


更新 2015-01-05     ECMAScript5 中的字符串文字 允许上述语法:

行终止符不能出现在字符串文字中,除非作为 LineContinuation 的一部分来生成空字符序列。 使行终止符成为字符串文字的 String 值一部分的正确方法是使用转义序列,例如 \n\u000A

Put the backslash at the end of the line:

alert("Please Select file\
 to delete");

Edit    I have to note that this is not part of ECMAScript strings as line terminating characters are not allowed at all:

A 'LineTerminator' character cannot appear in a string literal, even if preceded by a backslash \. The correct way to cause a line terminator character to be part of the string value of a string literal is to use an escape sequence such as \n or \u000A.

So using string concatenation is the better choice.


Update 2015-01-05    String literals in ECMAScript5 allow the mentioned syntax:

A line terminator character cannot appear in a string literal, except as part of a LineContinuation to produce the empty character sequence. The correct way to cause a line terminator character to be part of the String value of a string literal is to use an escape sequence such as \n or \u000A.

近箐 2024-07-20 07:20:04

ECMAScript 6 引入了模板字符串

模板字符串是允许嵌入表达式的字符串文字。
您可以使用多行字符串和字符串插值功能
他们。

例如:

alert(`Please Select file   
to delete`);

会提醒:

Please Select file   
to delete

ECMAScript 6 introduced template strings:

Template strings are string literals allowing embedded expressions.
You can use multi-line strings and string interpolation features with
them.

For example:

alert(`Please Select file   
to delete`);

will alert:

Please Select file   
to delete
黯然 2024-07-20 07:20:04

将绳子分成两段

alert ("Please select file " +
       "to delete");

Break up the string into two pieces 

alert ("Please select file " +
       "to delete");
初与友歌 2024-07-20 07:20:04

值得注意的是。 尝试过:

alert("Some \
    string \
    wrapped \
    across \
    mutliples lines.")

这有效。 然而,意外的是,最后一个反斜杠后面有一个空格字符(所有其他反斜杠都在行尾)。 这导致了 javascript 的错误! 不过,删除这个空格可以修复错误。

这是使用 Cordova 的 Android ADT。

Interesting to note. Tried:

alert("Some \
    string \
    wrapped \
    across \
    mutliples lines.")

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.

晚风撩人 2024-07-20 07:20:04

你可以使用

1:  alert("Please select file" +
2:        " to delete");

那应该可以

You can just use

1:  alert("Please select file" +
2:        " to delete");

That should work

剑心龙吟 2024-07-20 07:20:04

您可以将长字符串常量分解为逻辑块并将它们分配到数组中。 然后使用空字符串作为分隔符进行join

var stringArray = [
  '1. This is first part....',
  '2. This is second part.....',
  '3. Finishing here.'
];

var bigLongString = stringArray.join('');
console.log(bigLongString);

输出将是:

  1. 这是第一部分......2。 这是第二部分......3。 到这里就结束了。

这种方式会稍微影响性能,但会提高代码的可读性和可维护性。

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.

var stringArray = [
  '1. This is first part....',
  '2. This is second part.....',
  '3. Finishing here.'
];

var bigLongString = stringArray.join('');
console.log(bigLongString);

Output will be:

  1. This is first part....2. This is second part.....3. Finishing here.

There's a slight performance hit this way but you gain in code readability and maintainability.

梦在深巷 2024-07-20 07:20:04

反斜杠运算符不可靠。 尝试将此函数粘贴到浏览器控制台中:

function printString (){
  const s = "someLongLineOfText\
  ThatShouldNotBeBroken";
  console.log(s);
}

然后运行它。 由于函数内的常规(且正确)缩进,将包含两个额外的空格,从而导致 someLongLineOfText ThatShouldNotBeBroken

在这种情况下,即使使用反引号也无济于事。 始终使用串联“+”运算符来防止此类问题。

The backslash operator is not reliable. Try pasting this function in your browser console:

function printString (){
  const s = "someLongLineOfText\
  ThatShouldNotBeBroken";
  console.log(s);
}

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.

假扮的天使 2024-07-20 07:20:04

如果字符串分解为多行导致问题,这里是 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.)

咿呀咿呀哟 2024-07-20 07:20:04

我尝试了上述一些建议,但在 Chrome 代码检查器中收到了非法字符警告。 以下内容对我有用(尽管只在 Chrome 中进行了测试!)

alert('stuff on line 1\\nstuff on line 2);

结果就像...

stuff on line 1
stuff on line 2

注意双反斜杠!!...这似乎很重要!

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!)

alert('stuff on line 1\\nstuff on line 2);

comes out like...

stuff on line 1
stuff on line 2

NOTE the double backslash!!...this seems to be important!

一向肩并 2024-07-20 07:20:04

无需手动中断代码。
只需在要中断的地方添加 \n 即可。

alert ("Please Select file \n to delete");

这将显示如下警报

Please select file 
to delete.

No need of any manual break in code.
Just add \n where you want to break.

alert ("Please Select file \n to delete");

This will show the alert like

Please select file 
to delete.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文