为什么正则表达式在我的 JavaScript 中掌舵,我如何坚持"this is a string"?



我有这样的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抱怨那一行,超过一百行,我试图解决将撇号替换为"apos;"的问题。但是,它仍然在抱怨这一行,语法突出显示显然没有看到开头的双引号,而只看到正则表达式/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 对吐温的引用提出了投诉,称声明前缺少一个分号。我不确定这是从哪里来的;这可能是更高报价的二次损害吗?

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>";
<a href="/pain/">

第二个双引号未转义。 "不是/".

将这些

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

相关内容

最新更新