-
-
Save rawroland/175e7bc93cf63b3f8050ba1c312ffd9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// encode(decode) html text into html entity (assuming entity is hex) | |
var HtmlEntity = { | |
decode : function(str) { | |
return str.replace(/&#x([0-9A-F]+);/gi, function(match, dec) { | |
return String.fromCharCode(parseInt(dec, 16)); | |
}); | |
}, | |
encode : function(str) { | |
var buf = []; | |
for (var i = 0, length = str.length; i < length; i++) { | |
buf.push("&#x" + str[i].charCodeAt().toString(16) + ";"); | |
} | |
return buf.join(""); | |
} | |
}; | |
var entity = '高级程序设计'; | |
var str = '高级程序设计'; | |
console.log(HtmlEntity.decode(entity) === str); // true | |
console.log(HtmlEntity.encode(str) === entity); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment