标签:
/** * 中英文字符串截取 * @param str 要截取的字符串 * @param len 要截取的长度 * @param hasDot 是否在字符串末尾增加‘...‘ * @returns {string} * */ function mySubString (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 < strLength;i++) { singleChar = str.charAt(i).toString(); if(singleChar.match(chineseRegex) != null) { newLength += 2; }else { newLength++; } if(newLength > len) { break; } newStr += singleChar; } if(hasDot && strLength > len) { newStr += "..."; } return newStr; }
标签:
原文地址:http://www.cnblogs.com/raincha/p/4809178.html