Just another WordPress site

Convert HTML entity code script

Sometimes when dealing with API response we get HTML entity tag as well. For eg:

In order to fix this kind of issue we can use a simple javascript snippet.

function convertHTMLEntity(text){
    const span = document.createElement('span');
    return text
    .replace(/&[#A-Za-z0-9]+;/gi, (entity,position,text)=> {
        span.innerHTML = entity;
        return span.innerText;
    });
}
convertHTMLEntity(text);

The post Convert HTML entity code script appeared first on Santosh Shah.


Leave a Reply

Your email address will not be published. Required fields are marked *