Regexp-Utils:基本

ylbtech-Regexp-Utils:基本

 

1.返回頂部
一、
/**
    * 管理
    */
var utils = {
    hostUrl: "http://localhost:8023",
    isNull: function (s) {
        if (!s) return true;
        s = s.replace(/ /ig, ""); //去掉 
        var patrn = /^\s*$/;
        if (!patrn.exec(s)) return false;
        return true;
    },
    isDigit: function (s) {
        var patrn = /^[0-9]{1,20}$/;
        if (!patrn.exec(s)) return false;
        return true;
    },
    isMail: function (str) {
        var myReg = /^[_\-\.a-zA-Z0-9]+@([_\-a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;
        if (myReg.test(str))
            return true;
        return false;
    },
    isMobile: function (mobile) {
        if (this.isNull(mobile))
            return false;
        var mobileReg = !!mobile.match(/^(0|86|17951)?(13[0-9]|15[012356789]|17[3678]|18[0-9]|14[57])[0-9]{8}$/);
        return mobileReg;
    },
    Trim: function (str) {
        if (str === undefined) return "";
        return str.replace(/(^\s*)|(\s*$)/g, "");
    },
    isChar: function (s) {
        var patrn = /^[0-9a-zA-Z]*$/;
        if (!patrn.exec(s)) return false;
        return true;
    },
    isDigitFloat: function (s) {
        try {
            if (parseInt(s) === 0)
                return true;
        } catch (e) {

        }
        var patrn = /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/;
        if (patrn.exec(s)) return true;
        patrn = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/;
        if (patrn.exec(s)) return true;

        return false;
    },
    isUrl: function (urlString) {
        urlString = (urlString || '').toLowerCase();
        var regExp =
            /^(https?|http):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/;
        if (urlString.match(regExp)) return true;
        else return false;
    },
    formatDate: function (date, fmt) {
        if (!date) return "";
        if (!(date instanceof Date)) {
            try {
                date = new Date(date);
            } catch (ex) {
                return date;
            } finally {

            }
        }
        var o = {
            "M+": date.getMonth() + 1, //月份
            "d+": date.getDate(), //
            "h+": date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, //小時
            "H+": date.getHours(), //小時
            "m+": date.getMinutes(), //
            "s+": date.getSeconds(), //
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds() //毫秒
        };
        var week = {
            "0": "/u65e5",
            "1": "/u4e00",
            "2": "/u4e8c",
            "3": "/u4e09",
            "4": "/u56db",
            "5": "/u4e94",
            "6": "/u516d"
        };
        if (!fmt) fmt = "yyyy-MM-dd HH:mm:ss";
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        if (/(E+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[date.getDay() + ""]);
        }
        for (var k in o) {
            if (o.hasOwnProperty(k)) {
                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;
    },
    utcToLocal: function(ticks, format) {

        if(!ticks) {
            return "";
        }
        var t = new Date(ticks);
        var utc = new Date(t.getUTCFullYear(), t.getUTCMonth(), t.getUTCDate(), t.getUTCHours(), t.getUTCMinutes(), t.getUTCSeconds());

        if(!format) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        try {
            return this.formatDate(utc, format);
        } catch(e) {
            return "Invalid Date";
        }
    }
};

window.utils = utils;
    
二、
2.返回頂部
 
3.返回頂部
 
4.返回頂部
 
5.返回頂部
 
 
6.返回頂部
 
7.返回頂部
 
8.返回頂部
 
9.返回頂部
 
10.返回頂部
 
 
11.返回頂部
 
warn 做者:ylbtech
出處:http://ylbtech.cnblogs.com/
本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。
相關文章
相關標籤/搜索