Blog for web designers

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);

Santosh Shah

My name is Santosh Shah and blogs at santosh-shah.com


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.