JS中英文长度:
<script>
String.prototype.len= function ()
{
return this .replace(/[^\x00-\xff]/g, "rr" ).length;
}
</script>
JS截取:
String.prototype.sub = function (n) {
var r = /[^\x00-\xff]/g;
if ( this .replace(r, "mm" ).length <= n) return this ;
// n = n - 3;
var m = Math.floor(n/2);
for ( var i=m; i< this .length; i++) {
if ( this .substr(0, i).replace(r, "mm" ).length>=n) {
return this .substr(0, i) + "..." ; }
} return this ;
};
本文深入探讨了JavaScript中字符串的长度计算与截取方法,通过自定义原型方法实现复杂字符串处理,旨在提升开发者在实际项目中的代码效率与灵活性。
158

被折叠的 条评论
为什么被折叠?



