javascript - 使用 prompt() 时的不同输出
问题描述
与仅分配变量相比,为什么输出显示与提示不同?
迅速的:
var x = prompt(); // input "new \nline"
console.log(x); // output "new \nline"
分配变量:
var x = 'new \nline';
console.log(x)
//output
new line
解决方案
正如您在此处可用的 Mozilla 规范中看到的,提示消息将“字符串”作为输入,因此,如果您需要将转义字符“\”解析为字符串\\n
而不是换行符\n
,则将其解释为新行,您可以尝试以下操作:
var x = prompt(); // input "new \nline"
x = x.replace('\\n', '\n')
console.log(x);
输出:
new
line
推荐阅读
- python - 从一个数据框值与一个数据框列合并
- sql - 多个位置的 SQL 输入提示和 JOIN 问题的值相同
- c# - Razor 三元逻辑打印整个字符串而不是模型值?
- arrays - 无法快速将 NSNULL 转换为来自 API 的 NSString 响应
- docker - Docker,如何将特定于 docker 的文件版本复制到 WORKDIR
- javascript - 编写打乱函数的更简单方法
- php - PHP-FPM 记录对 /index.php Symfony 的访问
- linux - Grub找不到Windows 10
- database - ScyllaDB 上的主要压缩
- android - 当我尝试打开列表时应用程序崩溃