我有一个删除额外或特殊字符的功能:
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, "");
}
当我调用它时,我的这部分代码看起来像:
$(".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 ) );
});
但是它在控制台中的一些说法:SyntaxError: unterminated string literal
此字段中的数据看起来像
VINOVO - Lotti TERRENO Hobbystico - Campestre!!! <br />