汉字 编码格式处理

来源:互联网 发布:淘宝如何增加流量 编辑:程序博客网 时间:2024/05/24 05:44
  1. function ascii(str){
  2. return str.replace(/[^/u0000-/u00FF]/g,function($0){return escape($0).replace(/(%u)(/w{4})/gi,"//u$2")});
  3. }
  4. function unascii(str){
  5. return unescape(str.replace(///u/g,"%u"));
  6. }

 

  1. function convert(str)
  2. {
  3.   result = "";
  4.   for(i = 0 ; i < str.length ; i++) {
  5.     c = str.charAt(i);
  6.     if((' ' <= c && c <= '~') || (c == '/r') || (c == '/n')) {
  7.       if(c == '&') {
  8.         cstr = "&";
  9.       } else if(c == '<') {
  10.         cstr = "<";
  11.       } else if(c == '>') {
  12.         cstr = ">";
  13.       } else {
  14.         cstr = c.toString();
  15.       }
  16.     } else {
  17.       cstr = "&#" + c.charCodeAt().toString() + ";";
  18.     }
  19.       result = result + cstr;
  20.   }
  21.   return  result;
  22. }