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

常用JS扩展基础类
 /** 
         * 扩展基础类 
         * 得到字符串的长度,包括中文和英文 
         **/  
        String.prototype.charlen = function() {  
            var arr = this.match(/[^\x00-\xff]/ig);  
            return this.length + (arr == null ? 0 : arr.length);  
        }  
  
        /** 
         * 扩展基础类 
         * 格式化字符串${0} -> 参考printf %s 
         **/  
        String.prototype.format = function() {  
            var args = arguments;  
            return this.replace(/\$\{(\d+)\}/g,                
                function(m, i){  
                    return args[i];  
                });  
        }   
  
        /** 
         * 扩展基础类 
         * 字符串首尾去空格 
         **/  
        String.prototype.trim = function() {  
            return this.replace(/(^\s*)|(\s*$)/g, "");  
        }  
  
        /** 
         * 扩展基础类 
         * 字符串包含字符串判断 
         **/  
        String.prototype.contains = function(sub) {  
            return this.indexOf(sub) != -1;  
        }  
  
        /** 
         * 扩展基础类 
         * 字符串比较大小 
         **/  
        String.prototype.compare = function(b) {  
            if(!b)  
                return -1;  
  
            if(this.length != b.length)  
                return this.length - b.length;  
  
            var i = 0;  
            for (; i < this.length; i++){  
                var val = this.charCodeAt(i) - b.charCodeAt(i);  
                if(val != 0)  
                    return val;  
            }  
  
            return 0;  
        }  
  
        /** 
         * 扩展基础类 
         * 替换字符 
         **/  
        String.prototype.replaceLen = function(start, len, replaced) {  
            if(!len)  
                return this;  
  
            if(start >= this.length)  
                return this;  
  
            var returnSeg = '';  
            var returnSeg2 = '';  
            var i = 0;  
            for (; i < this.length; i++){  
                var c = this.charAt(i);  
                if(i < start)  
                    returnSeg += c;  
  
                if(i >= start + len)  
                    returnSeg2 += c;  
            }  
  
            return returnSeg + replaced + returnSeg2;  
        }  
  
        /** 
         * 扩展基础类 
         * 替换字符,这个在替换填入比较有用,比如***天***小时 替换为 <input />天<input />小时 
         **/  
        String.prototype.replaceChar = function(target, replaced, start) {  
            if(!target)  
                return this;  
  
            if(!start)  
                start = 0;  
  
            var returnVal = this.substring(0, start);  
            var index = 0;  
            for (var i = start; i < this.length; i++) {  
                var c = this.charAt(i);  
                target = typeof target == 'function' ? target.call(this, index) : target;  
                if (c == target) {  
                    returnVal += typeof replaced == 'function' ? replaced.call(this, index) : replaced;  
                    while (i < this.length - 1 && this.charAt(i + 1) == c) {  
                        i++;  
                    }  
                    index++;  
                }else{  
                    returnVal += c;  
                }  
            }  
  
            return returnVal;  
        }  
  
        /** 
         * 扩展基础类 
         * 克隆复制(简单copy而已) 
         **/  
        Array.prototype.clone = function(){  
            var arr = [];  
            var i = 0;  
            for(; i < this.length; i++){  
                switch(typeof this[i]){  
                    case 'object':  
                        var obj = {};  
                        for(key in this[i])  
                            obj[key] = this[i][key];  
                        arr.push(obj);  
                        break;  
                    default:  
                        arr.push(this[i]);  
                        break;  
                }  
            }  
            return arr;