個人代碼庫

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;
};
相關文章
相關標籤/搜索