javascript - 使用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 一起使用。
我该怎么做呢?
注意:我放了三个斜线,但我不知道为什么其中一个斜线会被自动删除。
解决方案
只需尝试替换button
并且send
不考虑之前的斜线,您应该会得到您想要的结果:
var a = str.replace('"button"', '\\\"button"').replace('"send', '\\\"send');
推荐阅读
- r - 用于 GLM 的 R 中的 AIC/AICc/BIC 公式
- playframework - Akka 并获取单例对象的 ActorRef
- bootstrap-4 - 如何正确编译引导文档并在本地打开?
- c++ - 从 100 递减到 0 时向量下标超出范围
- javascript - 在子对象键上对 javascript 对象数组进行排序
- apache-spark - 如何在 Spark 数据框中逐列或逐行验证大型 csv 文件
- angular - Angular Reactive Forms - FormArray 从另一个索引开始
- python - 在带有 Keras 的 Tensorflow 2.3 中使用具有多个嵌入输入的 GPU 时无法分配设备进行操作
- django - 通过多个页面重定向(登录->注册->登录)而不会丢失django第一页中url的下一个参数
- javascript - NodeJS - 在另一个类文件中访问数组