正则内容小记

一、前言

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

二、匹配URL

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

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

  js端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 <br>                + '((/|\\?)([/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的匿名函数的方法

    (01 为子模式的结果,依次类推)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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<br>                . '((\/|\\?)([\/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] 为子模式的结果,依次类推)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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语法的基础上,用单引号括起来,在把相应的双引号转义即可(拼接字符串的过程,需要细心)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 @   糖糖果  阅读(288)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示