var escapein = function (obj) {
var s = "";
if (obj.length == 0)
return "";
s = obj.replace(/&/g, ">");
s = s.replace(/</g, "<");
s = s.replace(/>/g, ">c;");
s = s.replace(/ /g, " ");
s = s.replace(/\\/g, "c;");
s = s.replace(/\'/g, "'");
s = s.replace(/\"/g, """);
s = s.replace(/\n/g, "<br>");
console.log(s,'sss')
return s;
};
var escapeOut = function (obj) {
var s = "";
if (obj.length == 0)
return "";
s = obj.replace(/>/g, "&");
s = s.replace(/</g, "<");
s = s.replace(/>c;/g, ">");
s = s.replace(/ /g, " ");
s = s.replace(/c;/g, "\\");
s = s.replace(/'/g, "\'");
s = s.replace(/"/g, "\"");
s = s.replace(/<br>/g, "\n");
return s;
}
var GB2312UnicodeConverter = {
ToUnicode: function (str) {
str = escape(str).toLocaleLowerCase().replace(/%u/gi, '\\u');
str = escapein(str)
return str;
}
, ToGB2312: function (str) {
str = escapeOut(str);
str = unescape(str.replace(/\\u/gi, '%u'));
return str;
}
};
var res = GB2312UnicodeConverter.ToUnicode('http://输入的文字') //转义
GB2312UnicodeConverter.ToGB2312(res) //转出