使用 javascript 将 HTML 字符实体转换回常规文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4338963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert HTML Character Entities back to regular text using javascript
提问by nuaavee
the questions says it all :)
问题说明了一切:)
eg. we have >
, we need >
using only javascript
例如。我们有>
,我们>
只需要使用 javascript
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.
更新:看来 jquery 是最简单的方法。但是,有一个轻量级的解决方案会很好。更像是一个能够自行完成此操作的函数。
回答by Gumbo
You could do something like this:
你可以这样做:
String.prototype.decodeHTML = function() {
var map = {"gt":">" /* , … */};
return this.replace(/&(#(?:x[0-9a-f]+|\d+)|[a-z]+);?/gi, function(function decodeEntities(s){
var str, temp= document.createElement('p');
temp.innerHTML= s;
str= temp.textContent || temp.innerText;
temp=null;
return str;
}
alert(decodeEntities('<'))
/* returned value: (String)
<
*/
, ) {
if ([0] === "#") {
return String.fromCharCode([1].toLowerCase() === "x" ? parseInt(.substr(2), 16) : parseInt(.substr(1), 10));
} else {
return map.hasOwnProperty() ? map[] : 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;
}
);
}
}
;
}
});
};
回答by kennebec
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;
};
}
回答by Nux
Here is a "class" for decoding whole HTML document.
这是一个用于解码整个 HTML 文档的“类”。
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) >
*/
Note that I used Gumbo's regexp for catching entities but for fully valid HTML documents (or XHTML) you could simpy use /&[^;]+;/g
.
请注意,我使用 Gumbo 的正则表达式来捕获实体,但对于完全有效的 HTML 文档(或 XHTML),您可以简单地使用/&[^;]+;/g
.
回答by CICDC
I know there are libraries out there, but here are a couple of solutions for browsers. These work well when placing html entity data strings into human editable areas where you want the characters to be shown, such as textarea's or input[type=text].
我知道那里有库,但这里有一些浏览器的解决方案。当将 html 实体数据字符串放入您希望显示字符的人类可编辑区域(例如 textarea 或 input[type=text])时,这些效果很好。
I add this answer as I have to support older versions of IE and I feel that it wraps up a few days worth of research and testing. I hope somebody finds this useful.
我添加这个答案是因为我必须支持旧版本的 IE,我觉得它结束了几天的研究和测试。我希望有人觉得这很有用。
First this is for more modern browsers using jQuery, Please note that this should NOT be used if you have to support versions of IE before 10 (7, 8, or 9) as it will strip out the newlines leaving you with just one long line of text.
首先,这是针对使用 jQuery 的更现代的浏览器,请注意,如果您必须支持 10(7、8 或 9)之前的 IE 版本,则不应使用此选项,因为它会去掉换行符,只留下一个长行的文本。
##代码##This next one is based on kennebec's work above, with some differences which are mostly for the sake of older IE versions. This does not require jQuery, but does still require a browser.
下一个基于 kennebec 的上述工作,其中有一些差异主要是为了旧的 IE 版本。这不需要 jQuery,但仍然需要浏览器。
##代码##