zoukankan      html  css  js  c++  java
  • 正则表达式锚匹配

      1 /*
      2     字符类:空白字符
      3 
      4 元字符/元符号        匹配情况
      5                     匹配null字符
      6                     匹配空格字符
      7     f                匹配进纸字符
      8     
                    匹配换行字符
      9     
                    匹配回车字符
     10     	                匹配制表字符
     11     s                匹配空白字符、空格、制表符和换行符
     12     S                匹配非空白字符
     13 
     14 
     15     字符类:锚字符
     16 
     17 元字符/元符号        匹配情况
     18     ^                行首匹配
     19     $                行尾匹配
     20     A                只有匹配字符串开始处
     21                     匹配单词边界,词在[]内时无效
     22     B                匹配非单词边界
     23     G                匹配当前搜索的开始位置
     24                     匹配字符串结束处或行尾
     25     z                只匹配字符串结束处
     26 
     27 
     28     字符类:记录字符
     29 
     30 元字符/元符号        匹配情况
     31     (string)        用于反向引用的分组
     32     1或$1            匹配第一个分组中的内容
     33     2或$2            匹配第二个分组中的内容
     34     3或$3            匹配第三个分组中的内容
     35     
     36 
     37     贪婪            惰性
     38     +                +?
     39     ?                ??
     40     *                *?
     41     {n}                {n}?
     42     {n,}            {n,}?
     43     {n,m}            {n,m}?
     44 
     45 */
     46 
     47 
     48 var pattern=/go ogle/;        //直接使用空格匹配
     49 var str='go ogle';
     50 alert(pattern.test(str));
     51 
     52 var pattern=/gosogle/;        //s表示空格匹配
     53 var str='go ogle';
     54 alert(pattern.test(str));
     55 
     56 var pattern=/google/;        //表示到达边界
     57 var str='google';
     58 alert(pattern.test(str));
     59 
     60 var pattern=/google|baidu|bing/;        //    |表示到匹配或选择模式
     61 var str='this is a bing';
     62 alert(pattern.test(str));
     63 
     64 var pattern=/google{4,7}/;            //表示e的4-8次
     65 var str='googleeee';
     66 alert(pattern.test(str));
     67 
     68 var pattern=/(google){4,8}/;        //分组,可以看成一个字符
     69 var str='googlegooglegooglegoogle';                    //表示google的4-8次
     70 alert(pattern.test(str));
     71 
     72 var pattern=/8(.*)8/;
     73 var str='This is a 8google8';
     74 alert(pattern.test(str));
     75 
     76 var pattern=/8(.*)8/;
     77 var str='This is a 8google8';
     78 pattern.test(str);        //需要先运行
     79 alert(RegExp.$1);        //RegExp.$1表示获取模式中第一个分组对应的匹配字符串(google)
     80 
     81 var pattern=/8(.*)8/;
     82 var str='This is a 8google8';
     83 document.write(str.replace(pattern,'<strong>$1</strong>'));    //$1表示分组获取字符串匹配到的内容
     84 
     85 var pattern=/(.*)s(.*)/;
     86 var str='google baidu';
     87 alert(str.replace(pattern,'$2 $1'));    //位置交换
     88 
     89 
     90 
     91 //关于贪婪和惰性
     92 
     93 var pattern=/[a-z]+/;        //这里使用了贪婪模式
     94 var str='fdsfasd';
     95 alert(str.replace(pattern,'1'));    //所有的字符串变成了1
     96 
     97 var pattern=/[a-z]+/g;        //开启全局,并且使用贪婪模式
     98 var str='fdsfasd';
     99 alert(str.replace(pattern,'1'));    //所有的字符串变成了1
    100 
    101 var pattern=/[a-z]+?/;        //这里使用了惰性模式
    102 var str='fdsfasd';
    103 alert(str.replace(pattern,'1'));    //只有第一个字符变成了1,后面没有匹配
    104 
    105 var pattern=/[a-z]+?/g;        //开启全局,并且使用惰性模式
    106 var str='fdsfasd';
    107 alert(str.replace(pattern,'1'));    //每一个字母都替换成了1
    108 
    109 var pattern=/8(.*)8/;        //使用了贪婪
    110 var str='8google8 8google8 8google8';    //匹配到了8google8 8google8 8google8
    111 document.write(str.replace(pattern,'<strong>$1</strong>'));//结果:<strong>google8 8google8 8google</strong>
    112 
    113 var pattern=/8(.*?)8/g;        //使用了惰性,开启全局
    114 var str='8google8 8google8 8google8';
    115 document.write(str.replace(pattern,'<strong>$1</strong>'));
    116 //结果:
    117 // <strong>google</strong>
    118 // <strong>google</strong>
    119 // <strong>google</strong>
    120 
    121 var pattern=/8([^8]*)8/g;        //另一种惰性,屏蔽了8的匹配,也就是两边的包含字符
    122 var str='8google8 8google8 8google8';
    123 document.write(str.replace(pattern,'<strong>$1</strong>'));
    124 //结果:
    125 // <strong>google</strong>
    126 // <strong>google</strong>
    127 // <strong>google</strong>
    View Code
    高否?富否?帅否? 否? 滚去学习!
  • 相关阅读:
    JavaEE——SpringMVC(11)--拦截器
    JavaEE——SpringMVC(10)--文件上传 CommonsMultipartResovler
    codeforces 460A Vasya and Socks 解题报告
    hdu 1541 Stars 解题报告
    hdu 1166 敌兵布阵 解题报告
    poj 2771 Guardian of Decency 解题报告
    hdu 1514 Free Candies 解题报告
    poj 3020 Antenna Placement 解题报告
    BestCoder5 1001 Poor Hanamichi(hdu 4956) 解题报告
    poj 1325 Machine Schedule 解题报告
  • 原文地址:https://www.cnblogs.com/baixc/p/3381589.html
Copyright © 2011-2022 走看看