How to decode HTML entities

2019-02-10 00:43发布

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条回答
三岁会撩人
2楼-- · 2019-02-10 01:30

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();
查看更多
登录 后发表回答