统计字符串有多少字节

      function testc(str) {
        //默认为0 var count = 0; for(var i = 0; i < str.length; i++) {
              //循环获取每一个字符 var c = str.charAt(i); //console.log(c);
              //正则,匹配中文, count + 2; 否则 + 1 if(/[^\u0000-\u00ff]/.test(c)) { count += 2; //console.log(count); } else { count += 1; } } console.log(count); }
      //调用 var jj = "一而点的wdd我w"; testc(jj);

  

posted @ 2018-06-19 09:43  adongP  阅读(428)  评论(0编辑  收藏  举报