欢迎光临
我们一直在努力

献丑啦–正则表达式练习器

建站超值云服务器,限时71元/月

    正则表达式的用途很广泛,但要熟练掌握就不是一件容易的事情了。为此,我编写了这个练习器用来帮助学习。
    请多指教!

*********将以下代码复制到 regexp.htm 即可 **********
<html>
<head>
<title>正则表达式练习器</title>
<meta name = 安徽 池州 统计局 徐祖宁 e-mail:czjsz@stats.gov.cn>
<script language="javascript">
function onmove() {
window.status = "("+window.event.clientx+","+window.event.clienty+")" + " :: "+document.location
}
</script>

<script language="javascript1.2">
var re = new regexp()  //建立正则表达式对象
var nextpoint = 0      //匹配时的偏移量
//设置正则表达式
function setpattern(form) {
  var mode
  if(form.chkmode.checked) mode = "gi"  //i:不分大小写 g:全局,好象没什么作用
  else mode = "g"
  re.compile(form.regexp.value,mode)
  nextpoint = 0
  form.reglist.value = ""
}
//检查是否有匹配
function findit(form) {
  setpattern(form)
  var input = form.main.value
  if (input.search(re) != -1) {
    form.output[0].checked = true    
  } else {
    form.output[1].checked = true    
  }
}
//检查匹配位置
function locateit(form) {
  setpattern(form)
  var input = form.main.value
  form.offset.value = input.search(re)
}
//检查所有的匹配情况
function execit(form) {
  if(nextpoint == 0 || ! form.scankmode.checked) {
    findit(form)
    form.reglist.value = ""
  }
  var key = true
  if(form.scankmode.checked) key = false
  do {
    var input = form.main.value
    var matcharray = re.exec(input.substr(nextpoint))
    if(matcharray) {
      for(var i=1;i<matcharray.length;i++)
        matcharray[i] = "$"+i+":"+matcharray[i]
      form.reglist.value = (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)
}
//设置当前使用的正则表达式
function setregexp(n) {
  var s = document.all.regexplist.value.split("\r\n")
  document.all.regexp.value = s[n*2-1]  //.replace("\r","")
  nextpoint = 0
}

//定义选择监视
var isnav = (navigator.appname == "netscape")
function showselection() {
  if (isnav) {
    var thetext = document.getselection()
  } else {
    var thetext = document.selection.createrange().text
  }
  if(thetext.length>0 && document.all.selechkmode.checked)
    document.all.regexp.value = thetext
}
if (isnav) {
    document.captureevents(event.mouseup)
}
document.onmouseup = showselection
</script>

</head>
<body style="font-size=9pt;" onmousemove=onmove()>
<form><table width=100% cellspacing=0 cellpadding=0><tr><td><font color=red>正规表达式练习器</font></td><td align=right><a href=mailto:czjsz_ah@stats.gov.cn>czjsz_ah@stats.gov.cn</a></td></tr></table>
<table width=100% broder=1 frame=above rules=none style="font-size:9pt;">
<tr><td width=50%  valign=top>
输入一些被寻找的正文:<br>
<textarea name="main" cols=58 rows=5 wrap="virtual" style="font-size:9pt;">
09-11-2001 09/11/2001 czjsz_ah@stats.gov.cn
asdff 12345 196.168.1.3 www.sohu.com ftp://www.chinaasp.com 2001.9.11 http://www.active.com.cn/club/bbs/bbsview.asp http://www.163.com/inden.htm
</textarea><br>
进行匹配的正规表达式:  忽略大小写<input type="checkbox" name="chkmode" checked style="font-size:8pt;height:18px"><br>
<textarea name="regexp" cols=51 rows=5 style="font-size:9pt;"></textarea>
<input type="button" value="清除" onclick="this.form.regexp.value=" style="font-size:8pt;height:18px"><br>
<input type="button" value="能找到吗?[regexobject.test(string)]" style="font-size:8pt;width:70%;height:18px" onclick="findit(this.form)">
<input type="radio" name="output" style="font-size:8pt;height:18px">yes
<input type="radio" name="output" style="font-size:8pt;height:18px">no <br>
<input type="button" value="在哪里?[string.search(regexobject)]" style="font-size:8pt;width:70%;height:18px" onclick="locateit(this.form)">
<input type="text" name="offset" size=4 style="font-size:8pt;height:18px">
</td>
<td valign=top>
测试用正则表达式列表: 
使用第<input type=text name=num size=2 value=1 style="font-size:8pt;height:18px">个<input type=button value=go onclick=setregexp(this.form.num.value) style="font-size:8pt;height:18px">
    允许复制<input type="checkbox" name="selechkmode" style="font-size:8pt;height:18px">
<textarea name="regexplist" cols=58 rows=14 wrap=off style="font-size:9pt;">
1.检查日期:
(1[0-2]|0?[1-9])[-./](0?[1-9]|[12][0-9]|3[01])[-./](\d\d\d\d))
2.检查数字:
([-+]?[0-9]+\.?[0-9]+)
3.检查url:
((http|ftp)://)?(((([\d]+\.)+){3}[\d]+(/[\w./]+)?)|([a-z]\w*((\.\w+)+){2,})([/][\w.~]*)*)
4.检查e-mail
\w+@((\w+[.]?)+)
</textarea>
</td></tr>
<tr><td valign=bottom>
<input type="button" value="有哪些?[regexobject.exec(string)]" style="font-size:8pt;width:70%;height:18px" onclick="execit(this.form)">
  单步<input type="checkbox" name="scankmode" style="font-size:8pt;height:18px"><br>
<textarea name="reglist" cols=58 rows=8 style="font-size:9pt;"></textarea>
</td>
<td valign=bottom>
匹配到的成分:(单步时可见)
<textarea name="matchlist" cols=58 rows=8 style="font-size:9pt;"></textarea>
</td></tr></table></form>
<script>
setregexp(1)
</script>
</body>
</html>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 献丑啦–正则表达式练习器
分享到: 更多 (0)

相关推荐

  • 暂无文章