欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

正则表达式练习器

程序员文章站 2022-01-01 12:26:08
复制代码 代码如下: 正则表达式练习器
复制代码 代码如下:

<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>


对正则表达式练习器的改进,原贴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)
}