用JavaScript 实现中文和UTF-8之间转换

中文转换为UTF-8:

var strInput = "我们是中国人";

var strutf8 = strInput.replace(/[^/u0000-/u00FF]/g,function($0){return escape($0).replace(/(%u)(/w{4})/gi,"&#x$2;")});

 

UTF-8转换为中文:

var strInput = "我们是中国人";

var strChin = unescape(strInput .replace(/&#x/g,'%u').replace(/;/g,''));

你可能感兴趣的:(JavaScript)