prototype 常用方法

来源:互联网 发布:mac重启 编辑:程序博客网 时间:2024/06/11 19:55

//函数名称:
//作    者:
//功能说明: 扩展string方法
//输入参数:
//输出参数:
//创建日期:
//=====================================================================
//此处为string类添加三个成员
String.prototype.Trim = function() { return this.replace(/(^/s*)|(/s*$)/g, ""); }
String.prototype.LTrim = function() { return this.replace(/(^/s*)/g,""); }
String.prototype.RTrim = function() { return this.replace(/(/s*$)/g,""); }
String.prototype.replaceAll = function(s1, s2) {
    //return this.replace(new RegExp(s1, "gm"), s2);

    var r = new RegExp(s1.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g,"\\$1"),"ig");
    return this.replace(r,s2);
}

http://blog.csdn.net/ddxkjddx/article/details/7084378


String.prototype.realLength = function() {return this.replace(/[^/x00-/xff]/g,"**").length; }

//Example:  var arr=[1,2,3];  var isExists= arr.in_array( 1 );

Array.prototype.in_array = function(e) {
    for (i = 0; i < this.length && this[i] != e; i++);
    return !(i == this.length);
}