使用javascript将HTML字符实体转换回常规文本
这些问题说明了一切:) 我们有使用javascript将HTML字符实体转换回常规文本,javascript,character-entities,Javascript,Character Entities,这些问题说明了一切:) 我们有,我们需要只使用javascript 更新:jquery似乎是最简单的解决方法。但是,如果有一个轻量级的解决方案就好了。更像是一个能够自己完成这项工作的函数。没有内置任何东西,但有许多库已经编写完成了这项工作 这是一个 还有一个是jQuery插件。您可以这样做: String.prototype.decodeHTML = function() { var map = {"gt":">" /* , … */}; return this.repla
,我们需要
只使用javascript
更新:jquery似乎是最简单的解决方法。但是,如果有一个轻量级的解决方案就好了。更像是一个能够自己完成这项工作的函数。没有内置任何东西,但有许多库已经编写完成了这项工作 这是一个
还有一个是jQuery插件。您可以这样做:
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;
}
});
};
功能解码实体{
var str,temp=document.createElement('p');
temp.innerHTML=s;
str=temp.textContent | | temp.innerText;
温度=零;
返回str;
}
警报(解码实体(“”))
/*返回值:(字符串)
<
*/
我知道有很多库,但这里有一些针对浏览器的解决方案。当将html实体数据字符串放置到您希望显示字符的可编辑区域(如textarea或input[type=text])中时,这些方法非常有效
我添加了这个答案,因为我必须支持旧版本的IE,我觉得这需要几天的研究和测试。我希望有人觉得这有用
首先,这是针对使用jQuery的更现代的浏览器,请注意,如果您必须支持10(7、8或9)之前的IE版本,则不应使用此选项,因为它将删除换行符,只留下一行长文本
if (!String.prototype.HTMLDecode) {
String.prototype.HTMLDecode = function () {
var str = this.toString(),
$decoderEl = $('<textarea />');
str = $decoderEl.html(str)
.text()
.replace(/<br((\/)|( \/))?>/gi, "\r\n");
$decoderEl.remove();
return str;
};
}
if(!String.prototype.HTMLDecode){
String.prototype.HTMLDecode=函数(){
var str=this.toString(),
$decoderEl=$('');
str=$decoderEl.html(str)
.text()
.replace(//gi,“\r\n”);
$decoderEl.remove();
返回str;
};
}
下一个版本基于kennebec的上述工作,其中一些差异主要是为了旧版IE。这不需要jQuery,但仍然需要浏览器
if (!String.prototype.HTMLDecode) {
String.prototype.HTMLDecode = function () {
var str = this.toString(),
//Create an element for decoding
decoderEl = document.createElement('p');
//Bail if empty, otherwise IE7 will return undefined when
//OR-ing the 2 empty strings from innerText and textContent
if (str.length == 0) {
return str;
}
//convert newlines to <br's> to save them
str = str.replace(/((\r\n)|(\r)|(\n))/gi, " <br/>");
decoderEl.innerHTML = str;
/*
We use innerText first as IE strips newlines out with textContent.
There is said to be a performance hit for this, but sometimes
correctness of data (keeping newlines) must take precedence.
*/
str = decoderEl.innerText || decoderEl.textContent;
//clean up the decoding element
decoderEl = null;
//replace back in the newlines
return str.replace(/<br((\/)|( \/))?>/gi, "\r\n");
};
}
/*
Usage:
var str = ">";
return str.HTMLDecode();
returned value:
(String) >
*/
if(!String.prototype.HTMLDecode){
String.prototype.HTMLDecode=函数(){
var str=this.toString(),
//创建用于解码的元素
decoderEl=document.createElement('p');
//如果为空,则为Bail,否则IE7将在
//或者从innerText和textContent中提取2个空字符串
如果(str.length==0){
返回str;
}
//将换行符转换为这里有一个用于解码整个HTML文档的“类”
HTMLDecoder = {
tempElement: document.createElement('span'),
decode: function(html) {
var _self = this;
html.replace(/&(#(?:x[0-9a-f]+|\d+)|[a-z]+);/gi,
function(str) {
_self.tempElement.innerHTML= str;
str = _self.tempElement.textContent || _self.tempElement.innerText;
return str;
}
);
}
}
注意,我使用Gumbo的regexp捕获实体,但对于完全有效的HTML文档(或XHTML),您可以简单地使用/&[^;]+/g
如果您需要此功能,则很可能您以错误的方式解决了问题。不这样做的原因是什么?在一种情况下需要此功能,即您的数据需要对HTML友好,以便显示,但可以保存到文本文件中并由用户下载。在这种情况下,确实需要此功能,因为se用户通常甚至不知道它是一个字符实体,不管它是哪一个。谷歌:跳过所有这些答案。最佳解决方案:简洁的解决方案。但我有一个问题-为什么要在第5行检查十六进制字符代码?@Nuavee:因为字符引用可以是十进制或十六进制表示法: 、
= ;
。这依赖于浏览器吗?我的意思是十六进制符号只适用于某些浏览器吗?@Nuavee:不,那就是。任何人都可以共享扩展映射变量吗?在不受信任的(用户输入的)文本上使用这是不安全的。请参阅此评论
HTMLDecoder = {
tempElement: document.createElement('span'),
decode: function(html) {
var _self = this;
html.replace(/&(#(?:x[0-9a-f]+|\d+)|[a-z]+);/gi,
function(str) {
_self.tempElement.innerHTML= str;
str = _self.tempElement.textContent || _self.tempElement.innerText;
return str;
}
);
}
}