首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用javascript转义字符串中的特定字符

使用javascript转义字符串中的特定字符
EN

Stack Overflow用户
提问于 2019-10-11 12:56:48
回答 1查看 47关注 0票数 0

我有一根这样的绳子:

代码语言: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\"]]}]";

我希望将所有\“按钮\\”替换为\“按钮\”,\“发送”替换为“发送”和“用)\”

到目前为止,我尝试的是:案例1:将“按钮\”替换为\“按钮”:

代码语言: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\"]]}]";

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

console.log(a)

这给出的输出为

代码语言:javascript
复制
[{"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:将“发送”替换为“发送”

代码语言: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\"]]}]";

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

console.log(a)

这给出了输出:

代码语言:javascript
复制
[{"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:当尝试替换)\“与)\”

代码语言:javascript
复制
var a = str.replace(/)\"/g, ')\\\"');

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

预期的最终产出是:

代码语言:javascript
复制
[{"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一起工作。

我该怎么做?

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-11 13:13:28

只需尝试替换buttonsend,而不考虑前面的斜杠,您就会得到想要的结果:

代码语言:javascript
复制
var a = str.replace('"button"', '\\\"button"').replace('"send', '\\\"send');
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58341747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档