转换示例:
& -> `&`
> -> `>`
有什么小的库函数可以处理这个问题吗?
我的实用腰带上总是有这个小功能:
function htmlDecode(input){
var e = document.createElement('div');
e.innerHTML = input;
return e.childNodes[0].nodeValue;
}
htmlDecode("&"); // "&"
htmlDecode(">"); // ">"
这对所有人都有效HTML 实体 http://www.w3.org/TR/html4/sgml/entities.html.
Edit:由于您不在 DOM 环境中,我认为您必须通过“困难”方式来完成此操作:
function htmlDecode (input) {
return input.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
//...
}
如果您不喜欢链式替换,您可以构建一个对象来存储您的实体,例如:
function htmlDecode (input) {
var entities= {
"&": "&",
"<": "<",
">": ">"
//....
};
for (var prop in entities) {
if (entities.hasOwnProperty(prop)) {
input = input.replace(new RegExp(prop, "g"), entities[prop]);
}
}
return input;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)