使用javascript将HTML Character Entities转换回常规文本 [英] Convert HTML Character Entities back to regular text using javascript
本文介绍了使用javascript将HTML Character Entities转换回常规文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题说明了一切:)
例如。我们有& gt;
,我们需要>
仅使用javascript
eg. we have >
, we need >
using only javascript
更新:看来jquery是最简单的方法。但是,拥有轻量级解决方案会很不错。更像是一个能够自行完成的功能。
Update: It seems jquery is the easy way out. But, it would be nice to have a lightweight solution. More like a function which is capable to do this by itself.
推荐答案
你可以这样做:
String.prototype.decodeHTML = function() {
var map = {"gt":">" /* , … */};
return this.replace(/&(#(?:x[0-9a-f]+|\d+)|[a-z]+);?/gi, function($0, $1) {
if ($1[0] === "#") {
return String.fromCharCode($1[1].toLowerCase() === "x" ? parseInt($1.substr(2), 16) : parseInt($1.substr(1), 10));
} else {
return map.hasOwnProperty($1) ? map[$1] : $0;
}
});
};
这篇关于使用javascript将HTML Character Entities转换回常规文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文