Pab*_*blo 9 html javascript html-entities
& -> `&`
> -> `>`
Run Code Online (Sandbox Code Playgroud)
任何可以处理这个的小库函数?
CMS*_*CMS 26
我的实用腰带上有这个小功能:
function htmlDecode(input){
var e = document.createElement('div');
e.innerHTML = input;
return e.childNodes[0].nodeValue;
}
htmlDecode("&"); // "&"
htmlDecode(">"); // ">"
Run Code Online (Sandbox Code Playgroud)
它适用于所有HTML实体.
编辑:由于您不在DOM环境中,我认为您必须通过"硬"方式来实现:
function htmlDecode (input) {
return input.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
//...
}
Run Code Online (Sandbox Code Playgroud)
如果您不喜欢链式替换,您可以构建一个对象来存储您的实体,例如:
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13450 次 |
| 最近记录: |