在JavaScript中转义单引号和双引号

Escaping single quote and double quote in JavaScript

本文关键字:单引号 JavaScript 转义      更新时间:2023-09-26

可能重复:
JavaScript字符串中的双引号

var array=["famous quote by Shakespear is",""to be or not to be""]

如何转义数组第二个元素中的块引号,该数组使用"来分隔元素?

您可以用反斜杠转义或在字符串周围使用单引号:

var array=["famous quote by Shakespear is","'"to be or not to be'""];
var array=["famous quote by Shakespear is",'"to be or not to be"'];

单引号和双引号可以互换,只要您正确配对即可。

使用反斜杠:

var str = "famous quote by Shakespear is, '"to be or not to be'"";
var str = 'famous quote by Shakespear is, ''to be or not to be''';

或者只是混合报价类型:

var str = "famous quote by Shakespear is, 'to be or not to be'";
var str = 'famous quote by Shakespear is, "to be or not to be"';