现在的位置: 首页 > 综合 > 正文

正则表达试练习器

2012年05月22日 ⁄ 综合 ⁄ 共 3359字 ⁄ 字号 评论关闭

 

<HTML>
<HEAD>
<TITLE>正则表达式练习器</TITLE>
<meta name = 厦门 e-mail:fanrsh@hotmail.com>
<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) {
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)
}


//设置当前使用的正则表达式
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></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=''" ><BR>
<INPUT TYPE="button" VALUE="能找到吗?[regexObject.test(string)]"  onClick="findIt(this.form)">
<INPUT TYPE="radio" NAME

抱歉!评论已关闭.