Detail refer to http://andrewu.co.uk/clj/entityencode/.
function TextToEntities(strPlainText, blnPartialEncodeOnly) {
var strPartial = [];
var strFull = [];
var intP = 0;
var intF = 0;
var objPartialRegExp = (new RegExp).compile("[\\w\\s]");
for (var intI=0; intI<strPlainText.length; ++intI) {
var strChar = strPlainText.charAt(intI);
var intChar = strChar.charCodeAt(0);
if (isNaN(intChar)) {
// IF CHAR FAILED TO DECODE, LEAVE AS CHAR
strPartial.push(strFull.push(strChar));
}
else {
var strEntity = "&#" + intChar + ";";
strFull.push(strEntity);
// IF CHAR WAS [a-zA-Z0-9_ \t] LEAVE AS CHAR, ELSE REPLACE WITH ENTITY
strPartial.push(objPartialRegExp.test(strChar) ? strChar : strEntity);
}
}
return (blnPartialEncodeOnly ? strPartial.join("") : strFull.join(""));
}
function EntitiesToText(strEncodedText) {
var strData = String(strEncodedText);
var objRegExp = (new RegExp).compile("&#(\\d+);", "ig");
/* FOR EACH MATCH TO ANY ENTITY, REPLACE THAT
ENTITY GLOBALLY WITH ITS SINGLE CHAR EQUIVALENT */
while(strData.match(objRegExp)) {
var strCharMatch = RegExp.$1;
var objRegExpMatch = new RegExp("&#" + strCharMatch + ";", "ig");
strData = strData.replace(objRegExpMatch, String.fromCharCode(strCharMatch));
}
return strData;
}
var strPartial = [];
var strFull = [];
var intP = 0;
var intF = 0;
var objPartialRegExp = (new RegExp).compile("[\\w\\s]");
for (var intI=0; intI<strPlainText.length; ++intI) {
var strChar = strPlainText.charAt(intI);
var intChar = strChar.charCodeAt(0);
if (isNaN(intChar)) {
// IF CHAR FAILED TO DECODE, LEAVE AS CHAR
strPartial.push(strFull.push(strChar));
}
else {
var strEntity = "&#" + intChar + ";";
strFull.push(strEntity);
// IF CHAR WAS [a-zA-Z0-9_ \t] LEAVE AS CHAR, ELSE REPLACE WITH ENTITY
strPartial.push(objPartialRegExp.test(strChar) ? strChar : strEntity);
}
}
return (blnPartialEncodeOnly ? strPartial.join("") : strFull.join(""));
}
function EntitiesToText(strEncodedText) {
var strData = String(strEncodedText);
var objRegExp = (new RegExp).compile("&#(\\d+);", "ig");
/* FOR EACH MATCH TO ANY ENTITY, REPLACE THAT
ENTITY GLOBALLY WITH ITS SINGLE CHAR EQUIVALENT */
while(strData.match(objRegExp)) {
var strCharMatch = RegExp.$1;
var objRegExpMatch = new RegExp("&#" + strCharMatch + ";", "ig");
strData = strData.replace(objRegExpMatch, String.fromCharCode(strCharMatch));
}
return strData;
}