国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Regex > 正文

正則表達式練習器

2020-03-16 21:24:25
字體:
來源:轉載
供稿:網(wǎng)友
正則表達式練習器,代碼如下:

  1. <HTML> 
  2. <HEAD> 
  3. <TITLE>正則表達式練習器</TITLE> 
  4. <meta name = 安徽 池州 統(tǒng)計局 徐祖寧 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. </script> 
  9. <SCRIPT LANGUAGE="JavaScript1.2"> 
  10. var re = new RegExp() //建立正則表達式對象 
  11. var nextpoint = 0 //匹配時的偏移量 
  12. //設置正則表達式 
  13. function setPattern(form) { 
  14. var mode 
  15. if(form.chkmode.checked) mode = "gi" //i:不分大小寫 g:全局,好象沒什么作用 
  16. else mode = "g" 
  17. re.compile(form.regexp.value,mode) 
  18. nextpoint = 0 
  19. form.reglist.value = "" 
  20. //檢查是否有匹配 
  21. function findIt(form) { 
  22. setPattern(form) 
  23. var input = form.main.value 
  24. if (input.search(re) != -1) { 
  25. form.output[0].checked = true  
  26. } else { 
  27. form.output[1].checked = true  
  28. //檢查匹配位置 
  29. function locateIt(form) { 
  30. setPattern(form) 
  31. var input = form.main.value 
  32. form.offset.value = input.search(re) 
  33. //檢查所有的匹配情況 
  34. function execIt(form) { 
  35. if(nextpoint == 0 || ! form.scankmode.checked) { 
  36. findIt(form) 
  37. form.reglist.value = "" 
  38. var key = true 
  39. if(form.scankmode.checked) key = false 
  40. do { 
  41. var input = form.main.value 
  42. var matchArray = re.exec(input.substr(nextpoint)) 
  43. if(matchArray) { 
  44. for(var i=1;i<matchArray.length;i++) 
  45. matchArray[i] = "$"+i+":"+matchArray[i] 
  46. form.reglist.value = (nextpoint+matchArray.index)+" => " + matchArray[0] +"/n"+form.reglist.value 
  47. form.matchlist.value = "$0:"+matchArray.join("/n") 
  48. nextpointnextpoint = nextpoint + matchArray.index + matchArray[0].length 
  49. }else { 
  50. if(!key) 
  51. form.reglist.value = "沒有找到/n" + form.reglist.value  
  52. form.matchlist.value = " " 
  53. nextpoint = 0 
  54. key = false 
  55. }while (key) 
  56. //設置當前使用的正則表達式 
  57. function setregexp(n) { 
  58. var s = document.all.regexplist.value.split("/r/n") 
  59. document.all.regexp.value = s[n*2-1] //.replace("/r","") 
  60. nextpoint = 0 
  61. //定義選擇監(jiān)視 
  62. var isNav = (navigator.appName == "Netscape") 
  63. function showSelection() { 
  64. if (isNav) { 
  65. var theText = document.getSelection() 
  66. } else { 
  67. var theText = document.selection.createRange().text 
  68. if(theText.length>0 && document.all.selechkmode.checked) 
  69. document.all.regexp.value = theText 
  70. if (isNav) { 
  71. document.captureEvents(Event.MOUSEUP) 
  72. document.onmouseup = showSelection 
  73. </SCRIPT> 
  74. </HEAD> 
  75. <BODY style="font-size=9pt;" OnMouseMove=OnMove()> 
  76. <FORM><table width=100cellspacing=0 cellpadding=0><tr><td><font color=red>正規(guī)表達式練習器</font></td><td align=right><a href=mailto:czjsz_ah@stats.gov.cn>czjsz_ah@stats.gov.cn</a></td></tr></table> 
  77. <table width=100broder=1 frame=above rules=none style="font-size:9pt;"> 
  78. <tr><td width=50valign=top> 
  79. 輸入一些被尋找的正文:<BR> 
  80. <TEXTAREA NAME="main" COLS=58 ROWS=5 WRAP="virtual" style="font-size:9pt;"> 
  81. 09-11-2001 09/11/2001 czjsz_ah@stats.gov.cn 
  82. 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 
  83. </TEXTAREA><BR> 
  84. 進行匹配的正規(guī)表達式:  忽略大小寫<INPUT TYPE="checkbox" NAME="chkmode" checked style="font-size:8pt;height:18px"><BR> 
  85. <TEXTAREA NAME="regexp" COLS=51 ROWS=5 style="font-size:9pt;"></TEXTAREA> 
  86. <INPUT TYPE="button" VALUE="清除" onClick="this.form.regexp.value=''" style="font-size:8pt;height:18px"><BR> 
  87. <INPUT TYPE="button" VALUE="能找到嗎?[regexObject.test(string)]" style="font-size:8pt;width:70%;height:18px" onClick="findIt(this.form)"> 
  88. <INPUT TYPE="radio" NAME="output" style="font-size:8pt;height:18px">Yes 
  89. <INPUT TYPE="radio" NAME="output" style="font-size:8pt;height:18px">No <BR> 
  90. <INPUT TYPE="button" VALUE="在哪里?[string.search(regexObject)]" style="font-size:8pt;width:70%;height:18px" onClick="locateIt(this.form)"> 
  91. <INPUT TYPE="text" NAME="offset" SIZE=4 style="font-size:8pt;height:18px"> 
  92. </td> 
  93. <td valign=top> 
  94. 測試用正則表達式列表:  
  95. 使用第<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"> 
  96.     允許復制<INPUT TYPE="checkbox" NAME="selechkmode" style="font-size:8pt;height:18px"> 
  97. <textarea NAME="regexplist" cols=58 rows=14 wrap=off style="font-size:9pt;"> 
  98. 1.檢查日期: 
  99. (1[0-2]|0?[1-9])[-./](0?[1-9]|[12][0-9]|3[01])[-./](/d/d/d/d)) 
  100. 2.檢查數(shù)字: 
  101. ([-+]?[0-9]+/.?[0-9]+) 
  102. 3.檢查URL: 
  103. ((http|ftp)://)?(((([/d]+/.)+){3}[/d]+(/[/w./]+)?)|([a-z]/w*((/./w+)+){2,})([/][/w.~]*)*) 
  104. 4.檢查E-mail 
  105. /w+@((/w+[.]?)+) 
  106. </textarea> 
  107. </td></tr> 
  108. <tr><td valign=bottom> 
  109. <INPUT TYPE="button" VALUE="有哪些?[regexObject.exec(string)]" style="font-size:8pt;width:70%;height:18px" onClick="execIt(this.form)"> 
  110.   單步<INPUT TYPE="checkbox" NAME="scankmode" style="font-size:8pt;height:18px"><BR> 
  111. <TEXTAREA NAME="reglist" COLS=58 ROWS=8 style="font-size:9pt;"></TEXTAREA> 
  112. </td> 
  113. <td valign=bottom> 
  114. 匹配到的成分:(單步時可見) 
  115. <TEXTAREA NAME="matchlist" COLS=58 ROWS=8 style="font-size:9pt;"></TEXTAREA> 
  116. </td></tr></table></FORM> 
  117. <script> 
  118. setregexp(1) 
  119. </script> 
  120. </BODY> 
  121. </HTML> 
  122.  
  123. 對正則表達式練習器的改進,原貼ID901680 
  124. 覆蓋原execIt函數(shù) 
  125. 修改后的execIt函數(shù)允許對多個正則表達式進行匹配(每個正則表達式一行),并對每一個匹配成分顯示出是第幾個正則表達式匹配的。 
  126. 這可視為語法分析的雛形,只要對匹配產(chǎn)生相應的動作。 
  127. function execIt(form) { 
  128. var mode 
  129. if(form.chkmode.checked) mode = "gi" 
  130. else mode = "g" 
  131. var regexpArray = form.regexp.value.split("/r/n") //獲取正則表達式到數(shù)組 
  132. if(nextpoint == 0) form.reglist.value = "" 
  133. var key = true 
  134. if(form.scankmode.checked) key = false 
  135. else nextpoint = 0 
  136. do { 
  137. var offs = 9999999999 
  138. var pos = -1 
  139. var input = form.main.value.substr(nextpoint) 
  140. //對每個正則表達式進行匹配 
  141. for(var i=0;i<regexpArray.length;i++) { 
  142. re.compile(regexpArray[i],mode) 
  143. var matchArray = re.exec(input) 
  144. if(matchArray) { 
  145. if(offs > matchArray.index) { 
  146. offs = matchArray.index 
  147. pos = i //保存距離起始位子最近的匹配 
  148. if(pos>=0) { 
  149. re.compile(regexpArray[pos],mode) 
  150. var matchArray = re.exec(input) 
  151. for(var i=1;i<matchArray.length;i++) 
  152. matchArray[i] = "$"+i+":"+matchArray[i] 
  153. form.reglist.value = "["+(pos+1)+"]"+(nextpoint+matchArray.index)+" => " + matchArray[0] +"/n"+form.reglist.value 
  154. form.matchlist.value = "$0:"+matchArray.join("/n") 
  155. nextpointnextpoint = nextpoint + matchArray.index + matchArray[0].length 
  156. }else { 
  157. if(!key) 
  158. form.reglist.value = "沒有找到/n" + form.reglist.value  
  159. form.matchlist.value = " " 
  160. nextpoint = 0 
  161. key = false 
  162. }while(key) 

上一篇:ubbcode簡單實用

下一篇:17種正則表達式

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 明水县| 历史| 大港区| 治多县| 东明县| 长宁区| 明光市| 永春县| 通城县| 盱眙县| 乌拉特中旗| 漯河市| 临西县| 霍山县| 阿克陶县| 新密市| 河津市| 油尖旺区| 独山县| 嵊泗县| 克山县| 佛教| 赣榆县| 萨迦县| 苏尼特左旗| 衡阳县| 渭南市| 海林市| 右玉县| 邹城市| 华池县| 瓦房店市| 大关县| 仙桃市| 宜丰县| 吴堡县| 茶陵县| 上虞市| 普兰县| 博湖县| 莱芜市|