How to decode HTML entities

狂风中的少年 提交于 2019-11-29 14:52:12

问题


I have string variable with HTML entities:

var str = 'Some text & 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?


回答1:


You can use built-in Xml Services:

var str = 'Some text & 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();


来源:https://stackoverflow.com/questions/11366021/how-to-decode-html-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!