JavaScript多行字符串 [英] JavaScript Multiline String

查看:77
本文介绍了JavaScript多行字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是:

像在PHP中一样,将多行字符串存储到变量中的JavaScript方法是什么?

What is the JavaScript method to store a multiline string into a variable like you can in PHP?

推荐答案

如果用多行字符串"表示包含换行符的字符串,则可以使用\n(换行符)将它们转义来编写:

If by 'multiline string' you mean a string containing linebreaks, those can be written by escaping them using \n (for newline):

var multilineString = 'Line 1\nLine 2';
alert(multilineString);
// Line 1
// Line 2

如果您的意思是,如何跨多行代码 编写一个字符串,则可以通过在行的末尾添加\反斜杠来继续该字符串:

If you mean, how can a string be written across multiple lines of code, then you can continue the string by putting a \ backslash at the end of the line:

var multilineString = 'Line \
1\nLine 2';
alert(multilineString);
// Line 1
// Line 2

这篇关于JavaScript多行字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆