• JS正则实例


    <html>
     <body>
      
     </body>
    </html>
    <script>
    var strSrc = "xxa1b01c001yya2b02c002zz";
    var regex = /a(d)b(d{2})c(d{3})/gi;
    var arr,count=0;
    while((arr=regex.exec(strSrc)) != null){
        display();
    }
    
    function display(){
        document.write("<p>这是正则表达式regex.source=/"+regex.source+"/gi对字符串RegExp.input=<br/>""+RegExp.input+""进行第"+(++count)+"次搜索的结果:<br/>");
        document.write("RegExp.index为"+RegExp.index+"<br/>");
        document.write("RegExp.lastIndex为"+RegExp.lastIndex+"<br/>");
        document.write("RegExp.lastMatch为"+RegExp.lastMatch+"<br/>");
        document.write("RegExp.lastParen为"+RegExp.lastParen+"<br/>");
        document.write("RegExp.leftContext为"+RegExp.leftContext+"<br/>");
        document.write("RegExp.$1为"+RegExp.$1+"<br/>");
        document.write("RegExp.$2为"+RegExp.$2+"<br/>");
        document.write("RegExp.$3为"+RegExp.$3+"<br/>");
        document.write("RegExp.$4为"+RegExp.$4+"<br/>");
        document.write("arr.input为"+arr.input+"<br/>");
        document.write("返回数组的元素个数arr.length为"+arr.length+"<br/>");
        document.write("返回的数组内容为【");
        for(var i=0; i<arr.length; i++){
            if(i < arr.length-1)
                document.write("""+arr[i]+"",");
            else
                document.write("""+arr[i]+""】");
        }
    }
    
    
    </script>

     得到结果如下:

    这是正则表达式regex.source=/a(d)b(d{2})c(d{3})/gi对字符串RegExp.input=
    "xxa1b01c001yya2b02c002zz"进行第1次搜索的结果:
    RegExp.index为2
    RegExp.lastIndex为11
    RegExp.lastMatch为a1b01c001
    RegExp.lastParen为001
    RegExp.leftContext为xx
    RegExp.$1为1
    RegExp.$2为01
    RegExp.$3为001
    RegExp.$4为
    arr.input为xxa1b01c001yya2b02c002zz
    返回数组的元素个数arr.length为4
    返回的数组内容为【"a1b01c001","1","01","001"】

    这是正则表达式regex.source=/a(d)b(d{2})c(d{3})/gi对字符串RegExp.input=
    "xxa1b01c001yya2b02c002zz"进行第2次搜索的结果:
    RegExp.index为13
    RegExp.lastIndex为22
    RegExp.lastMatch为a2b02c002
    RegExp.lastParen为002
    RegExp.leftContext为xxa1b01c001yy
    RegExp.$1为2
    RegExp.$2为02
    RegExp.$3为002
    RegExp.$4为
    arr.input为xxa1b01c001yya2b02c002zz
    返回数组的元素个数arr.length为4
    返回的数组内容为【"a2b02c002","2","02","002"】

      

    如果想系统的学习正则表达式,请移步我的 正则表达式从入门到高手 的视频课程:
    http://edu.51cto.com/sd/59587
  • 相关阅读:
    Objective-C马路成魔【14-关键C语言功能】
    js正则表达式语法
    Python 得到Twitter所有用户friends和followers
    error:stray&#39;243&#39;in program
    VC各种方法获得的窗口句柄
    新东方雅思词汇---5.2
    php中this,self,parent三个关键字的区别辨析
    英语影视台词---一、少年派的奇幻漂流
    英语常用单词分类---1
    amazeui的表单开关插件的自定义事件必须添加.bootstrapSwitch 命名空间,给了我们什么启示
  • 原文地址:https://www.cnblogs.com/kevin-yuan/p/3426114.html
Copyright © 2020-2023  润新知