如何解码HTML实体 [英] How to decode HTML entities

查看:117
本文介绍了如何解码HTML实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有HTML实体的字符串变量:

var str ='Some text& text';



我想将它转换(解码)为原始字符:

一些文字& text



JavaScript没有内置函数来实现想要的结果。我无法使用jQuery或DOM对象,因为我需要它在Google Apps脚本中工作。



我怎么用简单的方法做到这一点?

解决方案

您可以使用内置的Xml Services:

  var str ='Some text&文本'; 
var decode = XmlService.parse('< d> + str +'< / d>');
var strDecoded = decode.getElement()。getText();

或者您可以使用内置的E4X XML类。

  var str ='Some text&#x26;文本'; 
var decode = new XML('< d> + str +'< / d>');
var strDecoded = decode.toString();


I have string variable with HTML entities:

var str = 'Some text &#x26; text';

I want to convert (decode) it to original characters:

Some text & text.

JavaScript doesn't have built-in function to achieve wanted result. I can't use jQuery or DOM objects because I need it to work in Google Apps Script.

How can I do that in simple way?

解决方案

You can use built-in Xml Services:

var str = 'Some text &#x26; text';
var decode = XmlService.parse('<d>' + str + '</d>');
var strDecoded = decode.getElement().getText();

or you can use built-in E4X XML class.

var str = 'Some text &#x26; text';
var decode = new XML('<d>' + str + '</d>');
var strDecoded = decode.toString();

这篇关于如何解码HTML实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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