随笔-0  评论-0  文章-15  trackbacks-0
  1 正则表达式的用途很广泛,但要熟练掌握就不是一件容易的事情了。为此,我编写了这个练习器用来帮助学习。 
  2 请多指教! 
  3
  4 *********将以下代码复制到 RegExp.htm 即可 ********** 
  5 < HTML >  
  6 < HEAD >  
  7 < TITLE > 正则表达式练习器 </ TITLE >  
  8 < script  language ="JavaScript" >  
  9 function  OnMove() 
 10 window.status  =   " ( " + window.event.clientX + " , " + window.event.clientY + " ) "   +   "  ::  " + document.location 
 11 }
 
 12
</ script >  
 13
 14 < SCRIPT  LANGUAGE ="JavaScript1.2" >  
 15 var  re  =   new  RegExp()  // 建立正则表达式对象 
 16 var  nextpoint  =   0   // 匹配时的偏移量 
 17 // 设置正则表达式 
 18 function  setPattern(form) 
 19 var  mode 
 20 if (form.chkmode.checked) mode  =   " gi "   // i:不分大小写 g:全局,好象没什么作用 
 21 else  mode  =   " g "  
 22 re.compile(form.regexp.value,mode) 
 23 nextpoint  =   0  
 24 form.reglist.value  =   ""  
 25 }
 
 26 // 检查是否有匹配 
 27 function  findIt(form) 
 28 setPattern(form) 
 29 var  input  =  form.main.value 
 30 if  (input.search(re)  !=   - 1
 31 form.output[ 0 ].checked  =   true  
 32 }
  else  
 33 form.output[ 1 ].checked  =   true  
 34 }
 
 35 }
 
 36 // 检查匹配位置 
 37 function  locateIt(form) 
 38 setPattern(form) 
 39 var  input  =  form.main.value 
 40 form.offset.value  =  input.search(re) 
 41 }
 
 42 // 检查所有的匹配情况 
 43 function  execIt(form) 
 44 if (nextpoint  ==   0   ||   !  form.scankmode.checked) 
 45 findIt(form) 
 46 form.reglist.value  =   ""  
 47 }
 
 48 var  key  =   true  
 49 if (form.scankmode.checked) key  =   false  
 50 do  
 51 var  input  =  form.main.value 
 52 var  matchArray  =  re.exec(input.substr(nextpoint)) 
 53 if (matchArray) 
 54 for ( var  i = 1 ;i < matchArray.length;i ++
 55 matchArray[i]  =   " $ " + i + " : " + matchArray[i] 
 56 form.reglist.value  =  (nextpoint + matchArray.index) + "  =>  "   +  matchArray[ 0 + " \n " + form.reglist.value 
 57 form.matchlist.value  =   " $0: " + matchArray.join( " \n "
 58 nextpoint  =  nextpoint  +  matchArray.index  +  matchArray[ 0 ].length 
 59 }
else  
 60 if ( ! key) 
 61 form.reglist.value  =   " 没有找到\n "   +  form.reglist.value 
 62 form.matchlist.value  =   "   "  
 63 nextpoint  =   0  
 64 key  =   false  
 65 }
 
 66 }
while  (key) 
 67 }
 
 68 // 设置当前使用的正则表达式 
 69 function  setregexp(n) 
 70 var  s  =  document.all.regexplist.value.split( " \r\n "
 71 document.all.regexp.value  =  s[n * 2 - 1 // .replace("\r","") 
 72 nextpoint  =   0  
 73 }
 
 74
 75 // 定义选择监视 
 76 var  isNav  =  (navigator.appName  ==   " Netscape "
 77 function  showSelection() 
 78 if  (isNav) 
 79 var  theText  =  document.getSelection() 
 80 }
  else  
 81 var  theText  =  document.selection.createRange().text 
 82 }
 
 83 if (theText.length > 0   &&  document.all.selechkmode.checked) 
 84 document.all.regexp.value  =  theText 
 85 }
 
 86 if  (isNav) 
 87 document.captureEvents(Event.MOUSEUP) 
 88 }
 
 89 document.onmouseup  =  showSelection 
 90
</ SCRIPT >  
 91
 92 </ HEAD >  
 93 < BODY  style ="font-size=9pt;"  OnMouseMove =OnMove() >  
 94 < FORM >< table  width =100%  cellspacing =0  cellpadding =0 >< tr >< td >< font  color =red > 正规表达式练习器 </ font ></ td >< td  align =right >< href =mailto:czjsz_ah@stats.gov.cn > czjsz_ah@stats.gov.cn </ a ></ td ></ tr ></ table >  
 95 < table  width =100%  broder =1  frame =above  rules =none  style ="font-size:9pt;" >  
 96 < tr >< td  width =50%  valign =top >  
 97 输入一些被寻找的正文: < BR >  
 98 < TEXTAREA  NAME ="main"  COLS =58  ROWS =5  WRAP ="virtual"  style ="font-size:9pt;" >  
 99 09-11-2001 09/11/2001 czjsz_ah@stats.gov.cn 
100 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 
101 </ TEXTAREA >< BR >  
102 进行匹配的正规表达式:  忽略大小写 < INPUT  TYPE ="checkbox"  NAME ="chkmode"  checked style ="font-size:8pt;height:18px" >< BR >  
103 < TEXTAREA  NAME ="regexp"  COLS =51  ROWS =5  style ="font-size:9pt;" ></ TEXTAREA >  
104 < INPUT  TYPE ="button"  VALUE ="清除"  onClick ="this.form.regexp.value=''"  style ="font-size:8pt;height:18px" >< BR >  
105 < INPUT  TYPE ="button"  VALUE ="能找到吗?[regexObject.test(string)]"  style ="font-size:8pt;width:70%;height:18px"  onClick ="findIt(this.form)" >  
106 < INPUT  TYPE ="radio"  NAME ="output"  style ="font-size:8pt;height:18px" > Yes 
107 < INPUT  TYPE ="radio"  NAME ="output"  style ="font-size:8pt;height:18px" > No  < BR >  
108 < INPUT  TYPE ="button"  VALUE ="在哪里?[string.search(regexObject)]"  style ="font-size:8pt;width:70%;height:18px"  onClick ="locateIt(this.form)" >  
109 < INPUT  TYPE ="text"  NAME ="offset"  SIZE =4  style ="font-size:8pt;height:18px" >  
110 </ td >  
111 < td  valign =top >  
112 测试用正则表达式列表:  
113 使用第 < 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" >  
114     允许复制 < INPUT  TYPE ="checkbox"  NAME ="selechkmode"  style ="font-size:8pt;height:18px" >  
115 < textarea  NAME ="regexplist"  cols =58  rows =14  wrap =off  style ="font-size:9pt;" >  
116 1.检查日期: 
117 (1[0-2]|0?[1-9])[-./](0?[1-9]|[12][0-9]|3[01])[-./](\d\d\d\d)) 
118 2.检查数字: 
119 ([-+]?[0-9]+\.?[0-9]+) 
120 3.检查URL: 
121 ((http|ftp)://)?(((([\d]+\.)+){3}[\d]+(/[\w./]+)?)|([a-z]\w*((\.\w+)+){2,})([/][\w.~]*)*) 
122 4.检查E-mail 
123 \w+@((\w+[.]?)+) 
124 </ textarea >  
125 </ td ></ tr >  
126 < tr >< td  valign =bottom >  
127 < INPUT  TYPE ="button"  VALUE ="有哪些?[regexObject.exec(string)]"  style ="font-size:8pt;width:70%;height:18px"  onClick ="execIt(this.form)" >  
128   单步 < INPUT  TYPE ="checkbox"  NAME ="scankmode"  style ="font-size:8pt;height:18px" >< BR >  
129 < TEXTAREA  NAME ="reglist"  COLS =58  ROWS =8  style ="font-size:9pt;" ></ TEXTAREA >  
130 </ td >  
131 < td  valign =bottom >  
132 匹配到的成分:(单步时可见) 
133 < TEXTAREA  NAME ="matchlist"  COLS =58  ROWS =8  style ="font-size:9pt;" ></ TEXTAREA >  
134 </ td ></ tr ></ table ></ FORM >  
135 < script >  
136 setregexp( 1
137
</ script >  
138 </ BODY >  
139 </ HTML >  
140
141
142 对正则表达式练习器的改进,原贴ID901680 
143
144 覆盖原execIt函数 
145 修改后的execIt函数允许对多个正则表达式进行匹配(每个正则表达式一行),并对每一个匹配成分显示出是第几个正则表达式匹配的。 
146 这可视为语法分析的雏形,只要对匹配产生相应的动作。 
147
148 function execIt(form) { 
149 var mode 
150 if(form.chkmode.checked) mode = "gi" 
151 else mode = "g" 
152 var regexpArray = form.regexp.value.split("\r\n") //获取正则表达式到数组 
153
154 if(nextpoint == 0) form.reglist.value = "" 
155 var key = true 
156 if(form.scankmode.checked) key = false 
157 else nextpoint = 0 
158 do { 
159 var offs = 9999999999 
160 var pos = -1 
161 var input = form.main.value.substr(nextpoint) 
162 //对每个正则表达式进行匹配 
163 for(var i=0;i < regexpArray .length;i++) { 
164 re.compile(regexpArray[i],mode) 
165 var matchArray  = re.exec(input) 
166 if(matchArray) { 
167 if(offs  >  matchArray.index) { 
168 offs = matchArray.index 
169 pos = i //保存距离起始位子最近的匹配 
170
171
172
173 if(pos>=0) { 
174 re.compile(regexpArray[pos],mode) 
175 var matchArray = re.exec(input) 
176 for(var i=1;i < matchArray .length;i++) 
177 matchArray[i]  = "$" +i+":"+matchArray[i] 
178 form.reglist.value  = "[" +(pos+1)+"]"+(nextpoint+matchArray.index)+"  = >  " + matchArray[0] +"\n"+form.reglist.value 
179 form.matchlist.value = "$0:"+matchArray.join("\n") 
180 nextpoint = nextpoint + matchArray.index + matchArray[0].length 
181 }else { 
182 if(!key) 
183 form.reglist.value = "没有找到\n" + form.reglist.value 
184 form.matchlist.value = " " 
185 nextpoint = 0 
186 key = false 
187
188 }while(key) 
189
190
posted on 2007-01-18 03:17 Eternity 阅读(189) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。