node_man
node_man

Reputation: 1449

escape specific characters in a string using javascript

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.

Upvotes: 0

Views: 49

Answers (1)

helenej
helenej

Reputation: 401

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');

Upvotes: 2

Related Questions