正则内容小记

一、前言

  工作中遇到的正则,作为笔记简单记录一下,不定时更新。

二、匹配URL

  匹配常见的url,包括ip形式,端口,以及常见的字符串,如果没有匹配成功协议,默认添加http://;

注意js和php的正则表达式的不同,有些在php需要转义,在js端不需要,比如 / 符号

  js端

function isURL (str_url) { 
    var strRegex = '((https|http)://)?'
                + '('
                + '([0-9]{1,3}.){3}[0-9]{1,3}' // IP形式的URL- 199.194.52.184 
                + '|' // 允许IP和DOMAIN(域名) 
                + '([a-zA-Z0-9_!~*\'()-]+\\.)*' // 域名- www. 
                + '([a-zA-Z0-9-]{0,61})?[a-zA-Z0-9]\\.' // 二级域名 
                + '[a-z]{2,6}' // first level domain- .com or .museum 
                + ')'
                + '(:[0-9]{1,4})?' // 端口- :80 
+ '((/|\\?)([/a-zA-Z0-9_!~*\'()\\.;?:@&=+$,%#-]+)?)?'; var re=new RegExp(strRegex,"i"); res = str_url.match(re); if(res[1] === undefined && typeof(res[1])==='undefined'){ ret = str_url.replace(re,"<a href='http://$&' target='_blank'>$&</a>"); }else{ ret = str_url.replace(re,"<a href='$&' target='_blank'>$&</a>"); } // ret = str_url.replace(re,"<a href='$&' target='_blank'>$&</a>"); return ret; }

  上述的方法,只能匹配其中一个URL,要想全部匹配,可用下边的方法,string.replace的匿名函数的方法

    ($0 匹配到的完整结果,$1 为子模式的结果,依次类推)

function isURL (str_url) { 
    var strRegex = '((https|http)://)?'
                + '('
                + '([0-9]{1,3}\\.){3}[0-9]{1,3}' // IP形式的URL- 199.194.52.184 
                + '|' // 允许IP和DOMAIN(域名) 
                + '([a-zA-Z0-9_!~*\'()-]+\\.)*' // 域名- www. 
                + '([a-zA-Z0-9-]{0,61})?[a-zA-Z0-9]\\.' // 二级域名 
                + '[a-z]{2,6}' // first level domain- .com or .museum 
                + ')'
                + '(:[0-9]{1,4})?' // 端口- :80 
                + '((/|\\?)([/a-zA-Z0-9_!~*\'()\\.;?:@&=+$,%#-]+)?)?';

    var re = new RegExp(strRegex,"ig");

    ret = str_url.replace(re,function($0,$1){
        var replace;
        if(typeof($1) !== 'undefined' && $1 === 'http://'){
            replace = "<a href='"+$0+"' app_open='"+$0+"' target='_blank'>"+$0+"</a>";
        }else{
            replace = "<a href='http://"+$0+"' app_open='http://"+$0+"' target='_blank'>"+$0+"</a>";
        }
        return replace;
    });

    return ret;
}

 

 

  php端:

    public function isURL($str){
        $pattern = '/((https|http):\/\/)?' 
                . '('
                . '([0-9]{1,3}.){3}[0-9]{1,3}' // IP形式的URL- 199.194.52.184 
                . '|' // 允许IP和DOMAIN(域名) 
                . '([a-zA-Z0-9_!~*\'()-]+.)*' // 域名- www. 
                . '([a-zA-Z0-9-]{0,61})?[a-zA-Z0-9].' // 二级域名 
                . '[a-z]{2,6}' // first level domain- .com or .museum 
                . ')'
                . '(:[0-9]{1,4})?' // 端口- :80
. '((\/|\\?)([\/a-zA-Z0-9_!~*\'()\\.;?:@&=+$,%#-]+)?)?/i'; preg_match($pattern,$str,$matches); if(empty($matches[1])){ $replace = '<a href="http://$0" target="_blank">$0</a>'; }else{ $replace = '<a href="$0" target="_blank">$0</a>'; } // $replace = '<a href="$0" target="_blank">$0</a>'; $res = preg_replace($pattern, $replace, $str); return $res; }

  相对应的php端的匹配全部URL的写法

    (matches[0] 匹配到的完整结果,matches[1] 为子模式的结果,依次类推)

    function isURL($str){
        $pattern = '/((https|http):\/\/)?' 
                . '('
                . '([0-9]{1,3}\\.){3}[0-9]{1,3}' // IP形式的URL- 199.194.52.184 
                . '|' // 允许IP和DOMAIN(域名) 
                . '([a-zA-Z0-9_!~*\'()-]+\\.)*' // 域名- www. 
                . '([a-zA-Z0-9-]{0,61})?[a-zA-Z0-9]\\.' // 二级域名 
                . '[a-z]{2,6}' // first level domain- .com or .museum 
                . ')'
                . '(:[0-9]{1,4})?' // 端口- :80 
                . '((\/|\\?)([\/a-zA-Z0-9_!~*\'()\\.;?:@&=+$,%#-]+)?)?/i';

        $res = preg_replace_callback($pattern, function($matches){
            if(empty($matches[1])){
                $replace = '<a href="http://'.$matches[0].'" app_open="http://'.$matches[0].'" target="_blank">'.$matches[0].'</a>';
            }else{
                $replace = '<a href="'.$matches[0].'" app_open="'.$matches[0].'" target="_blank">'.$matches[0].'</a>';
            }
            return $replace;
        }, $str);
        return $res;

  由于低版本的php,导致匿名函数的时候,不支持,至此可使用从php4就开始支持的create_function,不过这个写起来确实蛋疼,

     第一个参数是 原匿名函数的参数,第二个是 原匿名函数的代码体

     需要注意的是:create_function中,使用单引号的话,变量可直接用单引号括起来,字符串需要区分,可用双引号括起来,如果需要双引号,注意转义

     实际上就是 在原来php语法的基础上,用单引号括起来,在把相应的双引号转义即可(拼接字符串的过程,需要细心)

    public function isURL($str){
        $pattern = '/((https|http):\/\/)?' 
                . '('
                . '([0-9]{1,3}\\.){3}[0-9]{1,3}' // IP形式的URL- 199.194.52.184 
                . '|' // 允许IP和DOMAIN(域名) 
                . '([a-zA-Z0-9_!~*\'()-]+\\.)*' // 域名- www. 
                . '([a-zA-Z0-9-]{0,61})?[a-zA-Z0-9]\\.' // 二级域名 
                . '[a-z]{2,6}' // first level domain- .com or .museum 
                . ')'
                . '(:[0-9]{1,4})?' // 端口- :80 
                . '((\/|\\?)([\/a-zA-Z0-9_!~*\'()\\.;?:@&=+$,%#-]+)?)?/i';
                
        $res = preg_replace_callback($pattern, create_function(
            '$matches', 
            'if(empty($matches[1])){'.
                '$replace = "<a href=\"http://".$matches[0]."\" app_open=\"http://".$matches[0]."\" target=\"_blank\">".$matches[0]."</a>";'.
            '}else{'.
                '$replace = "<a href=\"".$matches[0]."\" app_open=\"".$matches[0]."\" target=\"_blank\">".$matches[0]."</a>";'.
            '}'.
            'return $replace;'
            ), $str);
        return  $res;
    }

  

 

posted @ 2015-11-28 12:17  糖糖果  阅读(286)  评论(0编辑  收藏  举报