js实现超过长度的字符截取指定长度(中文字符算2个字符),超出部分以...显示
//超过长度的字符截取指定长度,超出部分以...显示 function subString(str, len) { var newLength = 0; var newStr = ""; var chineseRegex = /[^\x00-\xff]/g; var singleChar = ""; var strLength = str.replace(chineseRegex, "**").length; if (strLength > len) { for (var i = 0; i < strLength; i++) { singleChar = str.charAt(i).toString(); if (singleChar.match(chineseRegex) != null) { newLength += 2; } else { newLength++; } if (newLength > len) { break; } newStr += singleChar; } if (strLength > len) { newStr += "..."; } } else { newStr = str; } return newStr; }