·
电信镜像
·
网通镜像
首页
业界动态
网站运营
网页设计
网站编程
平面设计
服务器
数据库
热点专题
会员中心
站长论坛
热门关键字 :
芜湖
门户
百度
赚钱
互联网
智能
标题
2005-10-21
正则表达式练习器
来源: 本站收集整理 作者:佚名 评论
0 条
正则表达式的用途很广泛,但要熟练把握就不是一件轻易的事情了。为此,我编写了这个练习器用来帮助学习。
请多指教!
*********将以下代码复制到 RegExp.htm 即可*********
<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 ":" matchArray 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("rn") 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])[-./](dddd)) 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>
共2页: 上一页 1
[2]
下一页
(本文仅表明作者个人观点,不代表本站及其管理员立场.)
推荐
收藏
投稿
打印
返回
关闭
上一篇:
PHP进阶教程:实现网站的无限分类
下一篇:
放弃理想,未必能成就现实
评论加载中…
热门排行
·
让程序员立于不败之地的七种武器
·
多语言网站开发 不完全技术分析收录
·
C 类和接口的设计原则探讨
·
老生常谈:关于分页查询和性能问题
·
程序员如何缓解压力,保持心理健康
·
c语言难点透彻分析
·
程序员面试试题之关于继承
·
追MM与设计模式
·
用托管C 编写Windows服务
·
Mashups:Web 应用程序新成员
·
最受欢迎的十大程序设计语言排行榜
·
什么才是程序员的命根子
·
一刻钟精通正则表达式
·
正则表达式30分钟入门教程(第二版)
·
编写高性能Web应用程序的10个入门技巧
推荐文章
相关文章
·
放弃理想,未必能成就现实
·
一个程序员的奋斗历程
·
编程员行为规范守则
·
正则表达式经典
·
修炼一名程序员的职业水准
·
认清现实,才能找回理想
·
程序员应具备的素质
·
c语言10个经典小程序
·
揭开正则表达式语法的神秘面纱
·
准备选举自己最喜欢的开发工具
·
十八问:怎么才是喜欢编程
·
规划Web开发战略
·
程序员生涯之我见
·
编程开发规范
·
程序员口述:为别人编织快乐 自己的人生是乱
网站首页
-
网站地图
-
站长论坛
-
网站投稿
- -
网站管理
Copyright © 2008
芜湖站长站
All Rights Reserved
皖ICP备07500611号