0

I have a string like this :

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\"]]}]";

I want to replace all \"button\" with \\"button\\" , \"send with \\"send and )\" with )\\"

What I tried so far : case 1 : to replace \"button\" with \\"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)

This gives the output as

[{"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"]]}]

The above output has type=\"button\" hence JSON.parse will work in this.

case 2 : to replace \"send with \\"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)

This gives the output :

[{"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"]]}]

The above output doesnt even contain type=\"button\" or \"send . It removed the escape character. So JSON.parse will throw an error.

case 3 : when trying to replace )\" with )\\"

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

This throws an error : Uncaught SyntaxError: Invalid regular expression: /)\"/: Unmatched ')'

The expected final output is :

[{"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"]]}]

This final output will contain escaped double quotes inside of a string such that it will work with JSON.parse.

How do I do this?

Note: I had put three slashes but I have no idea why one of the slash is removed automatically.

1 Answer 1

2

Just try to replace button and send without taking into account the slash before and you should get the result you want:

var a = str.replace('"button"', '\\\"button"').replace('"send', '\\\"send');
Sign up to request clarification or add additional context in comments.

8 Comments

btw this only replaces the first occurence. How to replace all the occurences?
btw this only replaces the first occurence. How to replace all the occurences?
No problem! Just add the global modifier to replace all the occurences (add 'g' parameter to str.replace).
is this correct str.replace('"button"'g, '\\\"button"') ?
ok, sorry, it's apparently non standard. Then you should use str.replace(new RegExp('"button"', 'g'), '\\\"button"').
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.