解码& amp; amp;回到&在JavaScript中 [英] Decode & back to & in JavaScript

查看:2055
本文介绍了解码& amp; amp;回到&在JavaScript中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有像

这样的字符串

  var str ='One& amp;两个&三'; 

由Web服务器呈现为HTML。我需要将这些字符串转换为

 'One&两个&三''

目前,我正在做这件事(在jQuery的帮助下):

  $(document.createElement('div'))。html('{{driver.person.name}}')。text()

但是我有一种令人不安的感觉,那就是我做错了。
我曾尝试

  unescape(&)

但它似乎不起作用,decodeURI / decodeURIComponent也不起作用。



是还有其他的,更原生的和优雅的方式吗?

解决方案

解释HTML(文本和其他)来自JavaScript的是 DOMParser API中的HTML支持(在MDN中查看)。这允许您使用浏览器的本机HTML解析器将字符串转换为HTML文档。自2014年底以来,它在所有主流浏览器的新版本中得到了支持。



如果我们只想解码一些文本内容,我们可以将它作为唯一内容文档正文,解析文档,并取出其 .body.textContent



  var encodedStr ='hello& amp; amp; amp; amp; amp; amp; amp; amp; world'; var parser = new DOMParser; var dom = parser.parseFromString('<!doctype html>< body>'encodedStr,'text / html'); var decodedString = dom.body.textContent; console.log (decodedString);  



我们可以在 DOMParser 的草稿规范JavaScript未启用分析文档,因此我们可以在不担心安全问题的情况下执行此文本转换。


$ b


parseFromString (str,type)方法必须执行这些步骤,具体取决于类型


  • text / html



    解析 str c $ c> HTML解析器,并返回新创建的 Document



    脚本标志必须设置为禁用。


    注意

    脚本元素被标记为不可执行,并且 noscript 的内容被解析为标记。




这个问题超出了范围,但请注意如果您将解析的DOM节点本身(而不仅仅是文本内容)移动到实时文档DOM中,则可能会重新启用它们的脚本,并且可能存在安全问题。我没有研究过它,所以请谨慎行事。

I have strings like

var str = 'One &amp; two &amp; three';

rendered into HTML by the web server. I need to transform those strings into

'One & two & three'

Currently, that's what I am doing (with help of jQuery):

$(document.createElement('div')).html('{{ driver.person.name }}').text()

However I have an unsettling feeling that I am doing it wrong. I have tried

unescape("&amp;")

but it doesn't seem to work, neither do decodeURI/decodeURIComponent.

Are there any other, more native and elegant ways of doing so?

解决方案

A more modern option for interpreting HTML (text and otherwise) from JavaScript is the HTML support in the DOMParser API (see here in MDN). This allows you to use the browser's native HTML parser to convert a string to an HTML document. It has been supported in new versions of all major browsers since late 2014.

If we just want to decode some text content, we can put it as the sole content in a document body, parse the document, and pull out the its .body.textContent.

var encodedStr = 'hello &amp; world';

var parser = new DOMParser;
var dom = parser.parseFromString(
    '<!doctype html><body>' + encodedStr,
    'text/html');
var decodedString = dom.body.textContent;

console.log(decodedString);

We can see in the draft specification for DOMParser that JavaScript is not enabled for the parsed document, so we can perform this text conversion without security concerns.

The parseFromString(str, type) method must run these steps, depending on type:

  • "text/html"

    Parse str with an HTML parser, and return the newly created Document.

    The scripting flag must be set to "disabled".

    NOTE

    script elements get marked unexecutable and the contents of noscript get parsed as markup.

It's beyond the scope of this question, but please note that if you're taking the parsed DOM nodes themselves (not just their text content) and moving them to the live document DOM, it's possible that their scripting would be reenabled, and there could be security concerns. I haven't researched it, so please exercise caution.

这篇关于解码&amp; amp; amp;回到&amp;在JavaScript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆