Convert HTML Character Entities back to regular text using javascript

后端 未结 5 2074
梦毁少年i
梦毁少年i 2020-11-29 08:54

the questions says it all :)

eg. we have >, we need > using only javascript

Update: It seems jquery is t

5条回答
  •  孤街浪徒
    2020-11-29 09:21

    Here is a "class" for decoding whole HTML document.

    HTMLDecoder = {
        tempElement: document.createElement('span'),
        decode: function(html) {
            var _self = this;
            html.replace(/&(#(?:x[0-9a-f]+|\d+)|[a-z]+);/gi,
                function(str) {
                    _self.tempElement.innerHTML= str;
                    str = _self.tempElement.textContent || _self.tempElement.innerText;
                    return str;
                }
            );
        }
    }
    

    Note that I used Gumbo's regexp for catching entities but for fully valid HTML documents (or XHTML) you could simpy use /&[^;]+;/g.

提交回复
热议问题