个人代码库

JavaScript


string replaceAll方法:替换字符串中的全部匹配值

function replaceAll (str, oldValue, newValue) {
    newValue=newValue||"";
    return str.replace(new RegExp(oldValue, "gm"), newValue);
}

string trim方法:消除字符串首尾的空字符(如:空格 制表符 等)

var baseTrim = String.prototype.trim || function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
};

function trim(str){
    str=str||"";
    return baseTrim.call(str);
}

建立Guid

function newGuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
};

判断是不是int类型,或者是否能够转化为int类型

function isInt(value) {
    return isNaN(parseInt(value)) == false && parseInt(value) == value;
};
相关文章
相关标签/搜索