解码包含特殊 HTML 实体的字符串的正确方法是什么?
假设我从服务请求中返回一些 JSON,如下所示:
{
"message": "We're unable to complete your request at this time."
}
我不确定为什么撇号是这样编码的 ('
);我只知道我想解码它。
我脑海中浮现出一种使用 jQuery 的方法:
function decodeHtml(html) {
return $('<div>').html(html).text();
}
不过,这看起来(非常)hacky。有什么更好的办法呢?有“正确”的方法吗?
Say I get some JSON back from a service request that looks like this:
{
"message": "We're unable to complete your request at this time."
}
I'm not sure why that apostraphe is encoded like that ('
); all I know is that I want to decode it.
Here's one approach using jQuery that popped into my head:
function decodeHtml(html) {
return $('<div>').html(html).text();
}
That seems (very) hacky, though. What's a better way? Is there a "right" way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是我最喜欢的 HTML 字符解码方式。使用此代码的优点是标签也被保留。
示例: http://jsfiddle.net/k65s3/
输入:
输出:
This is my favourite way of decoding HTML characters. The advantage of using this code is that tags are also preserved.
Example: http://jsfiddle.net/k65s3/
Input:
Output:
如果您关心遗留兼容性,请不要使用 DOM 来执行此操作。使用 DOM 解码 HTML 实体(如当前接受的答案中所建议的)会导致 非现代浏览器上跨浏览器结果的差异。
对于一个强大的和根据 HTML 标准中的算法解码字符引用的确定性解决方案,请使用 he 库< /a>.来自其自述文件:
以下是您如何使用它:
免责声明:我是他图书馆。
有关更多信息,请参阅此 Stack Overflow 答案。
Don’t use the DOM to do this if you care about legacy compatibility. Using the DOM to decode HTML entities (as suggested in the currently accepted answer) leads to differences in cross-browser results on non-modern browsers.
For a robust & deterministic solution that decodes character references according to the algorithm in the HTML Standard, use the he library. From its README:
Here’s how you’d use it:
Disclaimer: I'm the author of the he library.
See this Stack Overflow answer for some more info.
如果你不想使用 html/dom,你可以使用正则表达式。我还没有测试过这个;但大致如下:
[编辑]
注意:这只适用于数字 html 实体,而不适用于像 &oring; 这样的东西。
[编辑2]
修复了功能(一些错别字),在这里测试:http://jsfiddle.net/Be2Bd/1 /
If you don't want to use html/dom, you could use regex. I haven't tested this; but something along the lines of:
[Edit]
Note: this would only work for numeric html-entities, and not stuff like &oring;.
[Edit 2]
Fixed the function (some typos), test here: http://jsfiddle.net/Be2Bd/1/
有 JS 函数来处理 &#xxxx 样式的实体:
GitHub 上的函数
There's JS function to deal with &#xxxx styled entities:
function at GitHub
jQuery 将为您编码和解码。
jQuery will encode and decode for you.
_.unescape
可以满足您的要求https://lodash.com/docs /#逃逸
_.unescape
does what you're looking forhttps://lodash.com/docs/#unescape
这是很好的答案。您可以将其与角度一起使用,如下所示:
This is so good answer. You can use this with angular like this: