前端常用的工具方法(一)

//1.判断是否为邮箱
        function isEmail(email) {
            return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(email)
        }
        var email = isEmail('[email protected]');
        console.log(email);//true
        //2.判断是否为手机号码
        function isMobile(mobliePhone) {
            return /^1[34578]\d{9}$/.test(mobliePhone);
        }
        var mobliePhone = isMobile('13033333333');
        console.log(mobliePhone);//true
        //3.电话号码
        function isPhone(phone) {
            return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(phone)
        }
        var phone = isPhone('10086');
        console.log(phone);//false
        //4.是否url地址
        function isUrl(url) {
            return /^http[s]?:\/\/.*/.test(url)
        }
        var url = isUrl('https://www.csdn.net/');
        console.log(url);//true
        //5.是否是字符串
        function isString(s) {
            return Object.prototype.toString.call(s).slice(8, -1) === 'String';
        }
        var str = isString(1);
        console.log(str);//false
        //6.是否是数字
        function isNumber(num) {
            return Object.prototype.toString.call(num).slice(8, -1) === 'Number';
        }
        var num = isNumber(22);
        console.log(num);//true
        //7.是否是boolean
        function isBoolean(bool) {
            return Object.prototype.toString.call(bool).slice(8, -1) === 'Boolean';
        }
        var bool = isBoolean(false);
        console.log(bool);//true
        //8.是否为函数
        function isFun(fun) {
            return Object.prototype.toString.call(fun).slice(8, -1) === 'Function';

        }
        var fun = isFun(function () { console.log('我是一个函数') });
        console.log(fun);//true
        //9.是否为null
        function isNull(n) {
            return Object.prototype.toString.call(n).slice(8, -1) === 'Null';
        }
        var n = isNull(NaN);
        console.log(n);//false
        //10.是否为undefined
        function isUndefined(under) {
            return Object.prototype.toString.call(under).slice(8, -1) == 'Undefined';
        }
        var under = isUndefined();
        console.log(under);//undefined
        //11.是否为对象
        function isObject(obj) {
            return Object.prototype.toString.call(obj).slice(8, -1) === 'Object';
        }
        var obj = isObject([1, 2, 3, 4]);
        console.log(obj);//false
        //12.是否为数组
        function isArray(arr) {
            return Object.prototype.toString.call(arr).slice(8, -1) === 'Array';
        }
        var arr = isArray([1, 2, 3, 4]);
        console.log(arr);//true

你可能感兴趣的:(JavaScript)