首页 > 解决方案 > 使用javascript转义字符串中的特定字符

问题描述

我有一个这样的字符串:

var str = "[{\"type\": \"text\", \"text\": \"I have below information for you:\"}, {\"type\": \"table\", \"columns\": [\"Product Name\", \"Status\", \"Comments\"], \"rows\": [[\"<button type=\"button\" onclick=\"send('Enfance Flower Detailed Sleeveless Rosette Dress - Maroon ')\">Enfance Flower Detailed Sleeveless Rosette Dress - Maroon</button>\", \"Confirmed\", \"Your order has been verified and you will receive updates when dispatched from our warehouse\"]]}]";

我想用 \\"button\\" 替换所有 \"button\" ,用 \\"send 和 )\" 替换 )\\"

到目前为止我尝试了什么:案例 1:用 \\"button\\" 替换 \"button\" :

var str = "[{\"type\": \"text\", \"text\": \"I have below information for you:\"}, {\"type\": \"table\", \"columns\": [\"Product Name\", \"Status\", \"Comments\"], \"rows\": [[\"<button type=\"button\" onclick=\"send('Enfance Flower Detailed Sleeveless Rosette Dress - Maroon ')\">Enfance Flower Detailed Sleeveless Rosette Dress - Maroon</button>\", \"Confirmed\", \"Your order has been verified and you will receive updates when dispatched from our warehouse\"]]}]";

var a = str.replace(/\"button\"/g, '\\\"button\\\"');

console.log(a)

这给出了输出

[{"type": "text", "text": "I have below information for you:"}, {"type": "table", "columns": ["Product Name", "Status", "Comments"], "rows": [["<button type=\"button\" onclick="send('Enfance Flower Detailed Sleeveless Rosette Dress - Maroon ')">Enfance Flower Detailed Sleeveless Rosette Dress - Maroon</button>", "Confirmed", "Your order has been verified and you will receive updates when dispatched from our warehouse"]]}]

上面的输出有 type=\"button\" 因此 JSON.parse 将在此工作。

案例 2:用 \\"send 替换 \"send

var str = "[{\"type\": \"text\", \"text\": \"I have below information for you:\"}, {\"type\": \"table\", \"columns\": [\"Product Name\", \"Status\", \"Comments\"], \"rows\": [[\"<button type=\"button\" onclick=\"send('Enfance Flower Detailed Sleeveless Rosette Dress - Maroon ')\">Enfance Flower Detailed Sleeveless Rosette Dress - Maroon</button>\", \"Confirmed\", \"Your order has been verified and you will receive updates when dispatched from our warehouse\"]]}]";

var a = str.replace(/\"button\"/g, '\\\"button\\\"');
var a = str.replace(/\"send"/g, '\\\"send');

console.log(a)

这给出了输出:

[{"type": "text", "text": "I have below information for you:"}, {"type": "table", "columns": ["Product Name", "Status", "Comments"], "rows": [["<button type="button" onclick="send('Enfance Flower Detailed Sleeveless Rosette Dress - Maroon ')">Enfance Flower Detailed Sleeveless Rosette Dress - Maroon</button>", "Confirmed", "Your order has been verified and you will receive updates when dispatched from our warehouse"]]}]

上面的输出甚至不包含 type=\"button\" 或 \"send 。它删除了转义字符。所以 JSON.parse 会抛出错误。

案例 3:当尝试用 )\\" 替换 )\" 时

var a = str.replace(/)\"/g, ')\\\"');

这会引发错误:Uncaught SyntaxError: Invalid regular expression: /)\"/: Unmatched ')'

预期的最终输出是:

[{"type": "text", "text": "I have below information for you:"}, {"type": "table", "columns": ["Product Name", "Status", "Comments"], "rows": [["<button type=\"button\" onclick=\"send('Enfance Flower Detailed Sleeveless Rosette Dress - Maroon ')\">Enfance Flower Detailed Sleeveless Rosette Dress - Maroon</button>", "Confirmed", "Your order has been verified and you will receive updates when dispatched from our warehouse"]]}]

这个最终输出将在字符串中包含转义的双引号,以便它可以与 JSON.parse 一起使用。

我该怎么做呢?

注意:我放了三个斜线,但我不知道为什么其中一个斜线会被自动删除。

标签: javascriptjqueryhtmljsonreplace

解决方案


只需尝试替换button并且send不考虑之前的斜线,您应该会得到您想要的结果:

var a = str.replace('"button"', '\\\"button"').replace('"send', '\\\"send');

推荐阅读