Reputation: 486
I am trying to remove the double quotes inside the string but was not able to do so.
var productName = "300" Table";
console.log(productName);
var newTemp = productName.replace('"', /'/g);
console.log(newTemp);
var requestData = JSON.stringify({
'product_name': productName,
'page_url': newTemp
});
var obj = JSON.parse(requestData);
console.log(obj);
It is throwing an error in the second line.
Upvotes: 0
Views: 7175
Reputation: 38502
From your coding pattern I think you need something like this just escape the inner double quotes("
) with a slash(\
) when you assign string to productName variable. Then replace the occurrence of double quotes with nothing i.e productName.replace(/"/g, "")
Full Code: Shorten after removing unnecessary JSON.stringify()
& JSON.parse()
var productName = "300\" Table";
var newTemp = productName.replace(/"/g, "");
console.log(`old productName = ${productName}, newTemp after replace " = ${newTemp}`);
var requestData = {
'product_name': productName,
'page_url': newTemp
};
console.log(requestData );
See Escape notation on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
Upvotes: 1