一些方法總結

/*
**兩數組並集交集差集
*/


let a = new Set([1, 2, 3]);
let b = new Set([3, 5, 2]); 

// 並集
let unionSet = new Set([...a, ...b]);
//[1,2,3,5]

// 交集
let intersectionSet = new Set([...a].filter(x => b.has(x)));
// [2,3]

// ab差集
let differenceABSet = new Set([...a].filter(x => !b.has(x)));
// [1]


//快速去重排序
let a = [1,2,5,3,1,3,4,3,4,5,6,1];
a = [...(new Set(a))];
a.sort(function (a, b) {
  return a - b;
});
console.log(a); //[1,2,4,5,6]



//一個json對象按兩個字段排序
var data = [{
    online:true,
    name:"BAILI003",
},{
    online:false,
    name:"123546789",
},{
    online:false,
    name:"000001",
},{
    online:false,
    name:"guangzhou1",
},{
    online:false,
    name:"7654321",
},{
    online:false,
    name:"BAODA002",
}];
data.sort(function(a, b){
    if (a.online === b.online) {
        var x = a.name; var y = b.name;
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    } else {
        return b.online - a.online;
    }
});


/**
***封裝日期格式
***/
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //
        "h+": this.getHours(),                   //小時
        "m+": this.getMinutes(),                 //
        "s+": this.getSeconds(),                 //
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};
const date = new Date(1502332290589);
console.log(date.Format('yyyy-MM-dd hh:mm:ss')); //2015-08-10 10:31:30



/****
**正則表達式
**/
//驗證手機
 /^1[34578]\d{9}$/;
//驗證身份證
/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
//驗證郵箱
/^\w+([-_.]\w+)*@(\w+[-.])+\w{2,5}$/;
//驗證價格
/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
//驗證中文
/^[\u0391-\uFFE5]+$/; 
//驗證網址
/^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/;

//驗證數字:
^[0-9]*$ 

//驗證n位的數字:
^\d{n}$  

//驗證至少n位數字:
^\d{n,}$ 

//驗證m-n位的數字:
^\d{m,n}$ 

//驗證零和非零開頭的數字:
^(0|[1-9][0-9]*)$ 

//驗證有兩位小數的正實數:
^[0-9]+(.[0-9]{2})?$ 

//驗證有1-3位小數的正實數:
^[0-9]+(.[0-9]{1,3})?$ 

//驗證非零的正整數:
^\+?[1-9][0-9]*$ 

//驗證非零的負整數:
^\-[1-9][0-9]*$ 

//驗證非負整數(正整數 + 0) 
^\d+$ 

//驗證非正整數(負整數 + 0) 
^((-\d+)|(0+))$ 

//驗證長度爲3的字符:
^.{3}$ 

//驗證由26個英文字母組成的字符串:
^[A-Za-z]+$ 

//驗證由26個大寫英文字母組成的字符串:
^[A-Z]+$ 

//驗證由26個小寫英文字母組成的字符串:
^[a-z]+$ 

//驗證由數字和26個英文字母組成的字符串:
^[A-Za-z0-9]+$ 

//驗證由數字、26個英文字母或者下劃線組成的字符串:
^\w+$ 
//驗證用戶名或暱稱常常用到:
 ^[\u4e00-\u9fa5A-Za-z0-9-_]*$  //只能中英文,數字,下劃線,減號

//驗證用戶密碼:
/^[a-zA-Z]\w{5,17}$/ //正確格式爲:以字母開頭,長度在6-18之間,只能包含字符、數字和下劃線。 

//驗證一年的12個月:
^(0?[1-9]|1[0-2])$ //正確格式爲:「01」-「09」和「1」「12」 

//驗證一個月的31天:
^((0?[1-9])|((1|2)[0-9])|30|31)$ //正確格式爲:0一、09和一、31。 

//整數:
^-?\d+$ 

//非負浮點數(正浮點數 + 0):
^\d+(\.\d+)?$ 

//正浮點數 
^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$ 

//非正浮點數(負浮點數 + 0)
 ^((-\d+(\.\d+)?)|(0+(\.0+)?))$ 

//負浮點數 
^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$ 

//浮點數 
^(-?\d+)(\.\d+)?$

//圖片
(.*)(\.jpg|\.bmp)$ //只能是jpg和bmp格式 
相關文章
相關標籤/搜索