基础正则

正则符号:
* 匹配前一个字符0次或者任意多次。
^ 匹配行首 ^hello :匹配以hello开头的字符。
$ 匹配行未 hello$ :匹配以hello结尾的字符。
. 匹配除换行符以为的任意一个字符。
[] 匹配括号中的任意一个字符 [abcd] :匹配abcd中的任意一个字符。
[^] 匹配除去括号中的任意一个字符 [^0-9] :匹配任何一个非数字的字符。
\ 转义符
\{n\} 表示其前面的字符恰好出现n次 。 [0-9]\{4\} :匹配4位数字。
\{n,\} 表示起前面的字符不小于n次 [0-9]\{\4,\}: 匹配4位及4位以上的字符。
\{n,m\} [0-9]\{n,m\} 最少n次最多m次

 

js的:

/**
 * [regexpStr 正则检测字符串是否是指定格式]
 * @param  {string} string [待检测字符串]
 * @param  {type} type   [检测类型]
 * @return {Boolean} 
 */
function regexpStr(string, type) {
    if (!type)
        var type = 'pwd';
    if (!in_array(type, ['integer', 'nninteger', 'zh-cn', 'email', 'url', 'pwd', 'post-code', 'zh-en', 'ip', 'id-number', 'mobile']))
        return false;
    if ($.trim(string) == '')
        return false;
    var regexp = {
        //匹配正整数
        'integer': /^[1-9]*[1-9][0-9]*$/,
        //匹配非负整数(正整数+0)
        'nninteger': /^\d+$/,
        //匹配中文
        'zh-cn': /^[\u4e00-\u9fa5]{2,4}$/,
        //匹配Email // 
        'email': /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/,
        //匹配网址URL
        'url': /^(f|ht){1}(tp|tps):\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?/,
        //匹配字母开头,5-16字符,字母数字下划线
        'pwd': /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/,
        //匹配中国邮政编码
        'post-code': /^[1-9]\d{5}$/,
        //匹配数字,字母,下划线,中文
        'zh-en': /^[\u4e00-\u9fa5A-Za-z0-9_]+$/,
        //匹配IP地址
        'ip': /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/,
        //匹配中国大陆身份证
        'id-number': /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$/,
        //匹配大陆手机号码
        'mobile': /(13[0-9]|14[0-9]|15[0-9]|18[0-9]|17[0-9])[0-9]{8}$/
    };
    return eval(regexp[type]).test(string) ? true : false;
}
if (!function_exists("regexpStr")) {

    /**
     * 常用正则
     */
    function regexpStr($string = null, $type = 'pwd') {
        if (empty($string))
            return false;
        $regexp = array(
            //匹配正整数
            'integer' => '/^[1-9]\d*$/',
            //匹配非负整数(正整数+0)
            'nninteger' => '/^\d+$/',
            //匹配中文
            'zh-cn' => "/^[\x{4e00}-\x{9fa5}]+$/u",
            //匹配Email // /^[0-9a-zA-Z]+(?:[\_\.\-][a-z0-9\-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\.[a-zA-Z]+$/i
            ///^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
            'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/',
            //匹配网址URL
            'url' => '/^(http:\/\/)?(https:\/\/)?([\w\d-]+\.)+[\w-]+(\/[\d\w-.\/?%&=]*)?$/',
            //匹配字母开头,5-16字符,字母数字下划线
            'pwd' => '/^[a-zA-Z][a-zA-Z0-9_]{5,15}$/',
            //匹配中国邮政编码
            'post-code' => '/^[1-9]\d{5}$/',
            //匹配数字,字母,下划线,中文
            'zh-en' => '/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u',
            //匹配IP地址
            'ip' => '/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/',
            //匹配中国大陆身份证
            'id-number' => '/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$/',
            //匹配大陆手机号码
//            'mobile' => '/^(?:13\d{9}|15[0|1|2|3|5|6|7|8|9]\d{8}|18[0|2|3|5|6|7|8|9]\d{8}|14[5|7]\d{8})$/',
            'mobile' => '/(13[0-9]|14[0-9]|15[0-9]|18[0-9]|17[0-9])[0-9]{8}$/',
            'YmdHis' => '/^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])(\s+(0?[0-9]|1[0-9]|2[0-3])\:(0?[0-9]|[1-5][0-9])\:(0?[0-9]|[1-5][0-9]))?$/',
            'Ymd' => '/^d{4}-d{2}-d{2}',
        );
        if (empty($regexp[$type]))
            return FALSE;
        preg_match($regexp[$type], $string, $res);
        return $res ? TRUE : FALSE;
    }

}
 

 

posted @ 2016-06-13 14:09  BiuBiuBiu666  阅读(151)  评论(0编辑  收藏  举报