码迷,mamicode.com
首页 > Web开发 > 详细

js截取中英文字符串、标点符号无乱码示例解读

时间:2016-04-20 18:03:30      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

<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++;
}
if(newLength > len){
break;
}
newStr += singleChar;
}

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

js截取中英文字符串、标点符号无乱码示例解读

标签:

原文地址:http://www.cnblogs.com/dyqblog/p/5413314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!