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

时间:2013-01-12 17:30:46

标签: javascript

  

可能重复:
  Double quote in JavaScript string

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

如何在使用"分隔元素的数组的第二个元素中转义blockquote?

2 个答案:

答案 0 :(得分:2)

使用反斜杠:

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"';

答案 1 :(得分:2)

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

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"'];

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