1. 程式人生 > >html拼接字符串中特殊字符(‘ “ 等的轉義問題)

html拼接字符串中特殊字符(‘ “ 等的轉義問題)

字符 TE entity cap 特殊字符 str ret map pla

function escapeHtml(string) {
var entityMap = {
"&": "&",
"<": "&lt;",
">": "&gt;",
‘"‘: ‘&quot;‘,
"‘": ‘&#39;‘,
"/": ‘&#x2F;‘
};
return String(string).replace(/[&<>"‘\/]/g, function (s) {
return entityMap[s];
});
}

var string5 = "<div class=‘div2‘ name=‘" + escapeHtml(str1) + "‘>test2222</div>";

html拼接字符串中特殊字符(‘ “ 等的轉義問題)