我有一根这样的绳子:
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\"]]}]";我希望将所有\“按钮\\”替换为\“按钮\”,\“发送”替换为“发送”和“用)\”
到目前为止,我尝试的是:案例1:将“按钮\”替换为\“按钮”:
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=\“按钮”,因此JSON.parse将在这个过程中工作。
案例2:将“发送”替换为“发送”
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=\“按钮\”或“发送”。它删除了转义字符。所以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一起工作。
我该怎么做?
注意:我放了三个斜杠,但我不知道为什么其中一个斜杠会自动移除。
发布于 2019-10-11 13:13:28
只需尝试替换button和send,而不考虑前面的斜杠,您就会得到想要的结果:
var a = str.replace('"button"', '\\\"button"').replace('"send', '\\\"send');https://stackoverflow.com/questions/58341747
复制相似问题