加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则方法 exec 和match以及test

发布时间:2020-12-14 01:23:44 所属栏目:百科 来源:网络整理
导读:var patt=new RegExp(/d+/g); var str="dfgdfg5465yhhgh65y65hh41"; var result; // var reset=patt.exec(str); while ((result=patt.exec(str))!= null){ // alert(patt.lastIndex) document.write(result); document.write("br /"); document.write(patt.
  var patt=new RegExp(/d+/g);
             var str="dfgdfg5465yhhgh65y65hh41";
             var result;
           //  var reset=patt.exec(str);
          while ((result=patt.exec(str))!= null){
                  // alert(patt.lastIndex)
                  document.write(result);
                  document.write("<br />");
                  document.write(patt.lastIndex);
                  document.write("<br />");           
              }
         document.write(str.match(patt));
         document.write(patt.test(str));

exec 只匹配一次 返回数组格式 match如果有全局匹配模式g 一直匹配到底。test返回布尔值true和false.

所以要exec全部匹配完需要进行循环。每匹配一次正则对象会返回一个lastIndex属性,下一次在执行的时候就从lastindex开始向后面匹配。没有匹配则返回null;

match方法 字符串方法。返回数组。匹配到了是否接着匹配依靠全局g标志。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读