网上找的JS截取字符串(含中文)

<script> 
/* 2007-11-28 XuJian */ 
//截取字符串 包含中文处理 
//(串,长度,增加...) 
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("字符串截取测试 www.jb51.net 是一个收藏精品学习资料的网站",10)); 
</script>

 

网上找的JS截取字符串(含中文),布布扣,bubuko.com

网上找的JS截取字符串(含中文)

上一篇:如何删除帝国cms面包屑导航中首页链接的/index.html


下一篇:mjson学习的简单例子分享