日期:2014-05-16  浏览次数:20350 次

js截取中英文字符串、标点符号无乱码
js截取中英文字符串、标点符号无乱码。
<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 < strLength;i++){ 
singleChar = str.charAt(i).toString(); 
if(singleChar.match(chineseRegex) != null){ 
newLength += 2; 
}else{ 
newLength++; 
} www.jbxue.com
if(newLength > len){ 
break; 
} 
newStr += singleChar; 
} 

if(hasDot && strLength > len){ 
newStr += "..."; 
} 
return newStr; 
} 
alert(subString("js截取中英文字符串无乱码",10,true)); 
</script>