// encode(decode) html text into html entity | |
var decodeHtmlEntity = function(str) { | |
return str.replace(/&#(\d+);/g, function(match, dec) { | |
return String.fromCharCode(dec); | |
}); | |
}; | |
var encodeHtmlEntity = function(str) { | |
var buf = []; | |
for (var i=str.length-1;i>=0;i--) { | |
buf.unshift(['&#', str[i].charCodeAt(), ';'].join('')); | |
} | |
return buf.join(''); | |
}; | |
var entity = '高级程序设计'; | |
var str = '高级程序设计'; | |
console.log(decodeHtmlEntity(entity) === str); | |
console.log(encodeHtmlEntity(str) === entity); | |
// output: | |
// true | |
// true |
This comment has been minimized.
This comment has been minimized.
This is fantastic, thank you. |
This comment has been minimized.
This comment has been minimized.
but what about ' ' |
This comment has been minimized.
This comment has been minimized.
decode it |
This comment has been minimized.
This comment has been minimized.
function htmlDecode(input){ |
This comment has been minimized.
This comment has been minimized.
xuebaofeng: Your solution does not work on a server using node.js, nor does it work on Pebble smartwatch. It only works in a browser. The solution provided works anywhere that JavaScript regular expressions are supported. Great solution. Thanks. I've been trying to pull this off all morning. |
This comment has been minimized.
This comment has been minimized.
Works well for encoding input from text fields straight into html using jQuery and this. |
This comment has been minimized.
This comment has been minimized.
Thanks, this solved an annoying issue with .net localization. |
This comment has been minimized.
This comment has been minimized.
I've written a little bit better implementation of decodeHtmlEntity that accept's html entities by name Gist |
This comment has been minimized.
This comment has been minimized.
Thank you! |
This comment has been minimized.
This comment has been minimized.
with Unicode support ( function encodeHtmlEntity(str) {
return str.replace(/[\u00A0-\u9999\<\>\&\'\"\\\/]/gim, function(c){
return '&#' + c.charCodeAt(0) + ';' ;
});
} or as bookmarklet (placed in your bookmark-toolbar to use whenever..) javascript:(function(){prompt("output:",prompt("∞ to HTML-Entity:","<div>❝Likė A Ƀoŝš❞ ©®</div>").replace(/[\u00A0-\u9999\<\>\&\'\"\\\/]/gim,function(c){return '&#'+c.charCodeAt(0)+';'}));return true;}());
|
This comment has been minimized.
This comment has been minimized.
Thanks so much. I just used it. |
This comment has been minimized.
This comment has been minimized.
Thanks a lot for this! |
This comment has been minimized.
This comment has been minimized.
function encodeHtmlEntity(str) {
var buf = '';
for (var i=0; i < str.length; i++) {
buf += '&#'+ str.charCodeAt(i) + ';';
}
return buf;
}; Come on man. |
This comment has been minimized.
This comment has been minimized.
Thank you |
This comment has been minimized.
This comment has been minimized.
Thank you for nice one |
This comment has been minimized.
This comment has been minimized.
Thank You. It was good of you to post it. |
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
TypeScript version: const decodeHtmlEntity = function (str: string) {
return str.replace(/&#(\d+);/g, function (match: string, dec: number) {
return String.fromCharCode(dec)
})
}
const encodeHtmlEntity = function (str: string) {
const buf = []
for (let i = str.length - 1; i >= 0; i--) {
buf.unshift(['&#', str.charCodeAt(i), ';'].join(''))
}
return buf.join('')
} |
This comment has been minimized.
very functional