首页 > 编程 > Regex > 正文

正则表达式练习器

2020-03-01 19:58:59
字体:
来源:转载
供稿:网友
class="codetitle">

 

 

 




 

//定义选择监视
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


 

正规表达式练习器 czjsz_ah@stats.gov.cn




 

输入一些被寻找的正文:



进行匹配的正规表达式:  忽略大小写





Yes
No



测试用正则表达式列表: 
使用第
    允许复制


  单步


匹配到的成分:(单步时可见)


 

 


对正则表达式练习器的改进,原贴ID901680

覆盖原execIt函数
修改后的execIt函数允许对多个正则表达式进行匹配(每个正则表达式一行),并对每一个匹配成分显示出是第几个正则表达式匹配的。
这可视为语法分析的雏形,只要对匹配产生相应的动作。

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 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[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)
}

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

图片精选