Javascript: Convert Numeric Character Reference to UTF8


Read more: PHP: Convert Numeric Character Reference to UTF8

Some functions to convert Numeric Character Reference (NCR) to UTF8:

Method 01: for Hex NCR

function convertHexNCRtoUTF8(ncr){
    return ncr.replace(/&#x([0-9A-F]+);/gi,function(b){
        return String.fromCodePoint(parseInt(b.slice(3,-1),16))
    })
}

Method 02: for decimal NCR

function convertDecimalNCRtoUTF8(ncr){
    return ncr.replace(/&#([0-9]+);/g,function(b){
        return String.fromCodePoint(parseInt(b.slice(3,-1)))
    })
}

Leave a Reply