Javascript常用定义方法

来源:互联网 发布:mysql查询结果中文乱码 编辑:程序博客网 时间:2024/06/05 20:20
//1.startsWith        String.prototype.startsWith = function (str) {            var reg = new RegExp("^" + str);            return reg.test(this);        } ;//2. trimString.prototype.trim = function(){var reExtraSpace = /^\s*(.*?)\s+$/;return this.replace(reExtraSpace,"$1");}//3. string.format//V1 methodString.prototype.format = function(){    var args = arguments;    return this.replace(/\{(\d+)\}/g,                      function(m,i){            return args[i];        });}//V2 staticString.format = function() {    if( arguments.length == 0 )        return null;    var str = arguments[0];    for(var i=1;i<arguments.length;i++) {        var re = new RegExp('\\{' + (i-1) + '\\}','gm');        str = str.replace(re, arguments[i]);    }    return str;}var a = "I Love {0}, and You Love {1},Where are {0}! {4}";alert(String.format(a, "You","Me"));alert(a.format("You","Me")); ====================================================2011-08-18 16:47


原创粉丝点击