• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

对正则表达式练习器的改进

发布: 2007-6-30 18:56 | 作者: admin | 来源: | 查看: 19次 | 进入软件测试论坛讨论

领测软件测试网 对正则表达式练习器的改进,原贴ID901680

覆盖原execIt函数
修改后的execIt函数允许对多个正则表达式进行匹配(每个正则表达式一行),并对每一个匹配成分显示出是第几个正则表达式匹配的。
这可视为语法分析的雏形,只要对匹配产生相应的动作。

function execIt(form) {
  var mode
  if(form.chkmode.checked) mode = "gi"
  else mode = "g"
  var regexpArray = form.regexp.value.split("\r\n")  //获取正则表达式到数组

  if(nextpoint == 0) form.reglist.value = ""
  var key = true
  if(form.scankmode.checked) key = false
  else nextpoint = 0
  do {
    var offs = 9999999999
    var pos = -1
    var input = form.main.value.substr(nextpoint)
    //对每个正则表达式进行匹配
    for(var i=0;i<regexpArray.length;i++) {
      re.compile(regexpArray[i],mode)
      var matchArray = re.exec(input)
      if(matchArray) {
        if(offs > matchArray.index) {
          offs = matchArray.index
          pos = i  //保存距离起始位子最近的匹配
        }
      }
    }
    if(pos>=0) {
      re.compile(regexpArray[pos],mode)
      var matchArray = re.exec(input)
      for(var i=1;i<matchArray.length;i++)
        matchArray[i] = "$"+i+":"+matchArray[i]
      form.reglist.value = "["+(pos+1)+"]"+(nextpoint+matchArray.index)+" => " + matchArray[0] +"\n"+form.reglist.value
      form.matchlist.value = "$0:"+matchArray.join("\n")
      nextpoint = nextpoint + matchArray.index + matchArray[0].length
    }else {
      if(!key)
        form.reglist.value = "没有找到\n" + form.reglist.value
      form.matchlist.value = " "
      nextpoint = 0
      key = false
    }
  }while(key)
}

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网