-7

我有 JavaScript 代码,例如:

 quotes = [];
 quotes[quotes.length] = "ABC quote";
 quotes[quotes.length] = "DEF quote";
 ...

其中一条特别是:

quotes[quotes.length] = "Mark Twain said, &apos;The secret source of humor itself is not joy but sorrow.  There is no humor in Heaven.&apos;<br /><a href=\"/pain\/">Humor Delivers Pain</a>";

Chrome 抱怨那条线,超过一百行,我试图解决将撇号替换为'的问题。但是,它仍然在抱怨这一行,语法高亮显然没有看到开头的双引号,而只有正则表达式/pain/">Humor Delivers Pain</

该表达式之前的所有内容都按预期突出显示为字符串,即使此类正则表达式字符串是样板子字符串;上一行,就像之前的那些:

quotes[quotes.length] = "The author was shaped by M.K. Gandhi. Here is why he is taking leave of Gandhi.<br /><a href=\"/gandhi/\">Farewell to Gandhi</a>";

即使这在引号内包含可能是正则表达式文字的内容,它也会被解释为预期的,作为包含转义引号的双引号字符串。

我正在尝试的页面是http://JonathansCorner.com/firestorm/。它旨在在页面顶部打印出通知,并更改链接。现在,它正在作为马克吐温的名言而崩溃。

Firefox 对 Twain 的引用提出了投诉,称声明前缺少分号。我不确定那是从哪里来的;这可能是更高报价造成的二次损害吗?

JavaScript 本身位于http://jonathanscorner.com/include/deprecation_notice.js

- 编辑 -

@Lightness,问题表现为前一行的两行样本,然后是绘制错误的行。有同样的错误,在前面的脚本行中指出,在:

quotes = [];
quotes[quotes.length] = "The author was shaped by M.K. Gandhi. Here is why he is taking leave of Gandhi.<br /><a href=\"/gandhi/\">Farewell to Gandhi</a>";
quotes[quotes.length] = "Mark Twain said, &apos;The secret source of humor itself is not joy but sorrow.  There is no humor in Heaven.&apos;<br /><a href=\"/pain\/">Humor Delivers Pain</a>";
4

1 回答 1

5
<a href=\"/pain\/">

第二个双引号没有转义。\"不是/"

将这些引号存储在纯文本文件或数据库中并自动转义它们可能会更可靠。手动转义大量文本是一件苦差事。

于 2012-12-31T18:36:46.087 回答