4 回答
TA贡献1802条经验 获得超4个赞
双反斜杠应该是单反斜杠。一个反斜杠转义了后面的字符,所以你用双倍做的是转义第二个反斜杠。它阻塞了 href,因为后面的引号结束了字符串,之后解析器将 url 中的“h”作为原始字符命中。
{ message: "...our website <a href=\\"https://test.com\\">here</a>" }
// ^ parser thinks the string ends here
// and doesn't know what to make of
// https://...
我的猜测是数据被两个不同的进程转义了两次(或者同一个进程运行了两次)。
假设示例:数据被创建,并在进入数据库的过程中被转义。所以现在所有引号前面都有一个反斜杠。然后对数据进行编辑并在数据库中更新记录,然后再次运行转义。但是输入字符串从第一次开始就已经包含反斜杠,并且因为反斜杠本身很特殊并且需要转义,所以当字符串在返回数据库的途中(再次)转义时,您最终会得到双反斜杠。
您可以通过在控制台中对字符串进行两次转义来看到这种情况。(这不是反斜杠,但它说明了问题):
const input = '"This string is quoted."';
const once = encodeURI(input);
// encodes the quotes as '%22'
// "%22This%20string%20is%20quoted.%22"
const twice = encodeURI(once);
// encodes the '%' in '%22' as '%25', and you end up with `%2522`
// "%2522This%2520string%2520is%2520quoted.%2522"
TA贡献1795条经验 获得超7个赞
使用模板字符串保存数据。
const json = `{"subtitle":"Information","desc":"Hi, Welcome.\\n<br><br>\\nYou can access our website <a href=\\"https://test.com\\">here</a>.\\n<br><br>\\nDont forget, cost: only $2!\\n<br>\\n<br>\\n<br>\\n<br>\\n Thankyou,<br>\\n Regards"}` console.log(JSON.parse(json))TA贡献1883条经验 获得超3个赞
json 中的双引号(值)只需要一个正斜杠(\)。所以你的json应该是
{"subtitle":"Information","desc":"Hi, Welcome.\\n <br><br>\\n You can access our website <a href=\"https://test.com\">here</a>.\\n <br><br>\\n Dont forget, cost: only $2! \\n <br>\\n <br>\\n <br>\\n <br>\\n Thankyou,<br>\\n Regards"}TA贡献1824条经验 获得超5个赞
非常感谢您的所有回答。有助于找出由双反斜杠引起的解析失败,双反斜杠转义了第二个反斜杠而不是双引号"。
数据有双反斜杠,因为这是来自克隆数据库的数据,我认为它发生在转换为 SQL 文件的过程中。
添加回答
举报
