一些方法总结

/*
**两数组并集交集差集
*/


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)$ //正确格式为:01、09和1、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格式 

 

posted @ 2017-08-10 11:20  katherine_web  阅读(112)  评论(0编辑  收藏  举报