js计算字符串的字节

方法一:

str.replace(/[^\x00-\xff]/g, 'xx').length 

方法二:

String.prototype.byteLen = function(){ 
            var len = 0, 
            i = this.length; 
            while(i--) 
                { 
                    len += (this.charCodeAt(i)>255 ? 2 : 1); 
                } 
            return len; 
}
调用:str.byteLen ()

你可能感兴趣的:(js)