Regular expression exerciser

Time:2022-4-26
Copy codeThe code is as follows:

<HTML>
<HEAD>
< title > regular expression exerciser < / Title >
< meta name = Anhui Chizhou Bureau of statistics Xu zuning e-mail: [email protected] >
<script language=”JavaScript”>
function OnMove() {
window.status = “(“+window.event.clientX+”,”+window.event.clientY+”)” + ” :: “+document.location
}
</script>

<SCRIPT LANGUAGE=”JavaScript1.2″>
Var re = new regexp() / / create regular expression object
Var nextpoint = 0 / / offset when matching
//Set regular expression
function setPattern(form) {
var mode
If (form. Chkmode. Checked) mode = “GI” / / I: regardless of case G: global. It doesn’t seem to work
else mode = “g”
re.compile(form.regexp.value,mode)
nextpoint = 0
form.reglist.value = “”
}
//Check for matches
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
}
}
//Check matching position
function locateIt(form) {
setPattern(form)
var input = form.main.value
form.offset.value = input.search(re)
}
//Check all matches
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] = “$”+i+”:”+matchArray[i]
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 = “not found \ n” + form reglist. value
form.matchlist.value = ” “
nextpoint = 0
key = false
}
}while (key)
}
//Sets the regular expression currently used
function setregexp(n) {
var s = document.all.regexplist.value.split(“\r\n”)
document.all.regexp.value = s[n*2-1] //.replace(“\r”,””)
nextpoint = 0
}

//Define selection monitoring
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 > regular expression exerciser < / font > < / td > < TD align = right > < a href = mailto: czjsz_ [email protected] >czjsz_ [email protected] </a></td></tr></table>
<table width=100% broder=1 frame=above rules=none style=”font-size:9pt;”>
<tr><td width=50% valign=top>
Enter some text to be found: < br >
<TEXTAREA NAME=”main” COLS=58 ROWS=5 WRAP=”virtual” style=”font-size:9pt;”>
09-11-2001 09/11/2001 [email protected]
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>
Normal expression for matching: ignore case < 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 = “clear” onclick = “this. Form. Regexp. Value = ”” style = “font size: 8pt; height: 18px” > < br >
< input type = “button” value = “can you find it? [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 = “where is it? [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>
List of regular expressions for testing:
Use the < input type = text name = num size = 2 value = 1 style = “font size: 8pt; height: 18px” > th < input type = button value = go onclick = setregexp (this. Form. Num.value) style = “font size: 8pt; height: 18px” >
Allow copying <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. Inspection date:
(1[0-2]|0?[1-9])[-./](0?[1-9]|[12][0-9]|3[01])[-./](\d\d\d\d))
2. Check the number:
([-+]?[0-9]+\.?[0-9]+)
3. Check URL:
((http|ftp)://)?(((([\d]+\.)+){3}[\d]+(/[\w./]+)?)|([a-z]\w*((\.\w+)+){2,})([/][\w.~]*)*)
4. Check e-mail
\[email protected]((\w+[.]?)+)
</textarea>
</td></tr>
<tr><td valign=bottom>
< input type = “button” value = “what are there? [regexobject. Exec (string)]” style = “font size: 8pt; width: 70%; height: 18px” onclick = “execit (this. Form)” >
Single step < 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>
Matched components: (visible in single step)
<TEXTAREA NAME=”matchlist” COLS=58 ROWS=8 style=”font-size:9pt;”></TEXTAREA>
</td></tr></table></FORM>
<script>
setregexp(1)
</script>
</BODY>
</HTML>

Improvement of regular expression exerciser, original post id901680

Overwrite the original execit function
The modified execit function allows multiple regular expressions to be matched (one line for each regular expression) and shows that each matching component is matched by the first regular expression.
This can be regarded as the rudiment of syntax analysis, as long as the corresponding action is generated for matching.

function execIt(form) {
var mode
if(form.chkmode.checked) mode = “gi”
else mode = “g”
var regexpArray = form. regexp. value. Split (“\ R \ n”) / / get regular expression to array

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)
//Match each regular expression
for(var i=0;i<regexpArray.length;i++) {
re.compile(regexpArray[i],mode)
var matchArray = re.exec(input)
if(matchArray) {
if(offs > matchArray.index) {
offs = matchArray.index
POS = I / / save the match closest to the start bit
}
}
}
if(pos>=0) {
re.compile(regexpArray[pos],mode)
var matchArray = re.exec(input)
for(var i=1;i<matchArray.length;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 = “not found \ n” + form reglist. value
form.matchlist.value = ” “
nextpoint = 0
key = false
}
}while(key)
}

Recommended Today

Accept these 7 plug-ins, which will make you even stronger when using vite

I believe that many partners have already started to use vue3 for development, and they must also use vite. These plug-ins I want to introduce today can make you even stronger when using vite for development. vite-plugin-restart Automatically restart the vite service by listening to file modification. The most common scenario is monitoringvite.config.jsand.env.developmentFile, we know […]