• JavaScript —— 字符串中使用正则表达式


      1 <!DOCTYPE html>
      2 <html lang="en">
      3 <head>
      4     <meta charset="UTF-8">
      5     <title>009字符串中使用正则表达式</title>
      6     <!-- 
      7         正则表达式中:g 表示的是全局模式
      8         正则表达式中:i 表示的是忽略大小写
      9         exec()方法 : 指在一个指定字符串中执行一个搜索匹配。返回一个结果数组或 null
     10 
     11     -->
     12     <script>
     13         /* 1、把一段字符串里面的数字提取出来
     14             var str = "中国移动:10086,中国联通:10010,中国电信:100000";
     15                 // 把里面所有的数字全部显示出来
     16             var array = str.match(/d{5}/g);
     17             console.log(array);*/
     18         
     19 
     20         // 2、提取字符串中的邮箱
     21            // var str1 = "123@xx.com,fangfang@valuedopinions.cn 285535644@qq.com2、    emailenglish.emailenglish.com 285535644@qq.com...";
     22            //     // w 匹配单个的非特殊符号(_)等价于[0-9a-zA-Z_] 
     23            //     // +表示前面表达式出现1次或多次
     24            // var array1 = str1.match(/w+@w+.w+(.w+)?/g);
     25            // console.log(array1);
     26 
     27         // 3、提取组正则表达式对象.$3
     28                // 提取日
     29            // var str2 = "2017-11-12";
     30            // var array2 = str2.match(/(d{4}[-])(d{2}[-])(d{2})/g);
     31            // // console.log(array2);
     32            // // 正则表达式对象.$3 那正则表达式中的第3组
     33            // console.log(RegExp.$3);
     34             
     35 
     36         // 4、匹配邮箱字符串中的各个部分
     37            // var email = "liusuyu@itcast.com.cn";
     38            // email.match(/([0-9a-zA-Z_.-]+)[@]([0-9a-zA-Z_-]+)(([.][a-zA-Z]+){1,2})/);
     39            // //[]  匹配其中的任意字符
     40            // // +  1~多
     41            // console.log(RegExp.$1);// 用户名
     42            // console.log(RegExp.$2);// itcast
     43            // console.log(RegExp.$3);// .com.cn
     44 
     45 
     46         // 5、使用replace()在全局替换字符串
     47            // var str = "小苏好帅,真的是太帅了,帅,就是很帅!";
     48            // str1 = str.replace(/帅/,"好玩");
     49            // //对字符串进行全局匹配
     50            // str2 = str.replace(/帅/g,"好玩");
     51            // // replace(/参1/,"参2") 参1:正则表达式中的字符 参2:参1要替换的字符
     52            // console.log(str1);//小苏好好玩,真的是太帅了,帅,就是很帅!
     53            // console.log(str2);//小苏好好玩,真的是太好玩了,好玩,就是很好玩!
     54 
     55 
     56         //6、在字符串全局去掉所有空格
     57             // var str ="  lalalala   ,xixixixi   ";
     58 
     59             // str1 = str.trim();//去掉字符串前后空格
     60             // console.log("----"+str1+"----"); //----lalalla   ,xixixix----
     61 
     62             // str2 = str.replace(/s+/,"");
     63             // console.log("----"+str2+"----");//----lalalala   ,xixixixi   ----
     64 
     65             // str3 = str.replace(/s+/g,"");
     66             // console.log("----"+str3+"----");//----lalalala,xixixixi----
     67         //7、将所有的Hh替换成S
     68             // var str = "HhpphH";
     69             // // 方法1:[] 匹配其中的任意字符
     70             // str1 = str.replace(/[hH]/g,"S");
     71             // console.log(str1);// SSppSS
     72             // // 方法2:i 忽略大小写
     73             // str2 = str.replace(/[h]/gi,"S");
     74             // console.log(str2);// SSppSS
     75     
     76             // //*使用正则表达式对象的方式
     77             // var reg  = new RegExp(/[h]/gi);
     78             // var str3 = str.replace(reg,"S");
     79             // console.log(str3);// SSppSS
     80         //
     81         var str = "中国移动:10086,中国联通:10010,中国电信:100000"; 
     82         //正则表达式对象.exec方法传入
     83         var reg = /d{5}/g;
     84         var array = reg.exec(str);// 通过正则表达式匹配当前字符串
     85 
     86            // console.log(array);
     87            // ["10086", index: 5, input: "中国移动:10086,中国联通:10010,中国电信:100000", groups: undefined]
     88            // console.log(reg.exec(str));
     89            // ["10010"...]
     90            // console.log(reg.exec(str));
     91            // ["10000"...] 
     92            // console.log(reg.exec(str));   // null(多写一个会出现null))
     93            // console.log(reg.exec(str));   // ["10086"...] 
     94            // ...
     95 
     96             while(array!=null){
     97                 // 输出匹配内容
     98                 console.log(array[0]);
     99                 // console.log(array);
    100                 array = reg.exec(str);
    101             }
    102             /*
    103             console.log(array[0])输出结果
    104              10086
    105              10010
    106              10000
    107              
    108             console.log(array)输出结果
    109             Array(1)
    110                0: "10086"
    111                groups: undefined
    112                index: 5
    113                input: "中国移动:10086,中国联通:10010,中国电信:100000"
    114                length: 1__proto__:Array(0)
    115             Array(1)
    116                0: "10010"
    117                groups: undefined
    118                index: 16
    119                input: "中国移动:10086,中国联通:10010,中国电信:100000"
    120                length: 1
    121                __proto__: Array(0)
    122             Array(1)
    123                0: "10000"
    124                groups: undefined
    125                index: 27
    126                input: "中国移动:10086,中国联通:10010,中国电信:100000"
    127                length: 1
    128                __proto__: Array(0)
    129             */
    130     </script>
    131 </head>
    132 <body>
    133     
    134 </body>
    135 </html>
  • 相关阅读:
    grunt安装
    RequireJS实例分析
    Linux下解压rar文件
    windows(64位)下使用curl命令
    RequireJS学习资料汇总
    Linux下firefox安装flash player插件
    caj转pdf——包含下载链接
    《社会化营销:人人参与的营销力量》—— 读后总结
    《税的真相》—— 读后总结
    基于代理的数据库分库分表框架 Mycat实践
  • 原文地址:https://www.cnblogs.com/sylys/p/11629782.html
Copyright © 2020-2023  润新知