超过长度的字符截取指定长度,超出部分以...显示
$(function () { subString('abcdef你好!Hello World', 6); }) //超过长度的字符截取指定长度,超出部分以...显示,中文字符是两个字符长度 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; }