I have a function for removing extra or special chars:
function escapeHtml(unsafe) {
return unsafe
.replace(/\r?\n|\r/g, "")
.replace(/(\r\n|\n|\r)/gm," ")
.replace(/\s+/g," ")
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'")
.replace(/!/g, "");
}
While I am calling it and this part of my code looks like:
$(".product_json", data).each(function(){
var thisH = $(this).html();
var myNewString = eval( '('+ thisH +')');
var toBeEscaped = myNewString.item_description;
var escapedString = escapeHtml(toBeEscaped);
myNewString.item_description = escapedString;
myNewString = JSON.stringify(myNewString);
console.log(myNewString);
//product_json.push( jQuery.parseJSON( myNewString ) );
});
But some how it says in console: SyntaxError: unterminated string literal
Data in this field looks like
VINOVO - Lotti TERRENO Hobbystico - Campestre!!! <br />
You assign a value to
thisHin this line:And then running it through
eval()(surrounded by parentheses) in this line:Use a debugger to check the value of
thisHwhen the SyntaxError is triggered. Is it valid JavaScript when surrounded by parentheses? If not, that’s your problem.