문제

I've a JavaScript code that extracts JSON strings from other pages from my blog (Blogger), but many special characters in those strings are as &#?????;, where ????? is a number up to 5 digits, or as something like \74br /\76, which should be a <br />.

Both come mixed in the same string, and both seem to be ASCII, the first one being decimal/html and the second one being octal.

How can I decode this mess to their respective characters by using JavaScript? Is there any existing function or proper solution for this?

도움이 되었습니까?

해결책

These should get you started

function decodeHtmlNumeric( str ) {
    return str.replace( /&#([0-9]{1,7});/g, function( g, m1 ){
        return String.fromCharCode( parseInt( m1, 10 ) );
    }).replace( /&#[xX]([0-9a-fA-F]{1,6});/g, function( g, m1 ){
        return String.fromCharCode( parseInt( m1, 16 ) );
    });
}

function decodeOctal( str ) {
    return str.replace( /\\([0-7]+)/g, function( g, m1 ) {
        return String.fromCharCode( parseInt( m1, 8 ) );
    });
}
           //Double \\ = one backslash 
decodeOctal("\\74br /\\76"); //"<br />"
decodeHtmlNumeric("&#255;"); //"ÿ"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top