js截取字符串并且区分汉字字母,一个汉字辨别为两个字节. function substr(str, len) { if (!str || !len) { return ''; } // 预期计数:中文2字节,英文1字节 var a = 0; // 循环计数 var i = 0; // 临时字串 var temp = ''; for (i = 0; i < str.length; i++) { if (str.charCodeAt(i) > 255) { // 按照预期计数增加2 a += 2;
<script> function subString(str, len, hasDot) { var newLength = 0; var newStr = ""; var chineseRegex = /[^\x00-\xff]/g; var singleChar = ""; var strLength = str.replace(chineseRegex,"**").length; for(var i = 0;i < st