?
?
?
?
//定義選擇監(jiān)視
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
?
?
對正則表達(dá)式練習(xí)器的改進(jìn),原貼ID901680
覆蓋原execIt函數(shù)
修改后的execIt函數(shù)允許對多個正則表達(dá)式進(jìn)行匹配(每個正則表達(dá)式一行),并對每一個匹配成分顯示出是第幾個正則表達(dá)式匹配的。
這可視為語法分析的雛形,只要對匹配產(chǎn)生相應(yīng)的動作。
function execIt(form) {
var mode
if(form.chkmode.checked) mode = "gi"
else mode = "g"
var regexpArray = form.regexp.value.split("/r/n") //獲取正則表達(dá)式到數(shù)組
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)
//對每個正則表達(dá)式進(jìn)行匹配
for(var i=0;i
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
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)
}
新聞熱點(diǎn)
疑難解答