首页 > 编程 > Regex > 正文

正则表达式练习器

2020-03-16 21:24:25
字体:大 中 小
来源:转载
供稿:网友
正则表达式练习器,代码如下:

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

上一篇:ubbcode简单实用

下一篇:17种正则表达式

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表