CopyCode The Code is as follows:
<HTML>
<Head>
<Title> Regular Expression exercise </title>
<Meta name = Anhui Chizhou Bureau of Statistics Xu zuning 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 () // create a regular expression object
VaR nextpoint = 0 // offset for matching
// Set the Regular Expression
Function setpattern (form ){
VAR mode
If (Form. chkmode. Checked) mode = "Gi" // I: case-insensitive G: Global, it seems useless
Else mode = "G"
Re. Compile (Form. Regexp. Value, Mode)
Nextpoint = 0
Form. reglist. value = ""
}
// Check for matching
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 the matching position
Function locateit (form ){
Setpattern (form)
VaR input = form. Main. Value
Form. offset. value = input. Search (re)
}
// Check all matching conditions
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.exe C (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 = "\ n not found" + form. reglist. Value
Form. matchlist. value = ""
Nextpoint = 0
Key = false
}
} While (key)
}
// Set the currently used Regular Expression
Function setregexp (n ){
VaR S = Document. All. regexplist. value. Split ("\ r \ n ")
Document. All. Regexp. value = s [N * 2-1] //. Replace ("\ r ","")
Nextpoint = 0
}
// Select monitoring for definition
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 exercise </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>
Enter the searched body: <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>
Regular Expression for matching: case-insensitive <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 be found? [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? [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 used for testing:
Use the <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. Check Date:
(1 [0-2] | 0? [1-9]) [-./] (0? [1-9] | [12] [0-9] | 3 [01]) [-./] (\ D ))
2. Check the number:
([-+]? [0-9] + \.? [0-9] +)
3. Check the URL:
(HTTP | FTP )://)? ([\ D] + \.) +) {3} [\ D] + (/[\ W./] + )?) | ([A-Z] \ W * (\. \ W +) {2,}) ([/] [\ W. ~] *)*)
4. Check email
\ W + @ (\ W + [.]?) +)
</Textarea>
</TD> </tr>
<Tr> <TD valign = bottom>
<Input type = "button" value = "What are there? Using regexobject.exe C (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 one step)
<Textarea name = "matchlist" Cols = 58 rows = 8 style = "font-size: 9pt;"> </textarea>
</TD> </tr> </table> </form>
<SCRIPT>
Setregexp (1)
</SCRIPT>
</Body>
</Html>
For the improvement of the regular expression exercise, the original post id901680
Overwrite the original execit Function
The modified execit function allows matching of multiple regular expressions (one row for each regular expression), and displays the number of regular expressions matching each matching component.
This can be seen as a prototype 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") // obtain the regular expression to the 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.exe C (input)
If (matcharray ){
If (offs> matcharray. Index ){
Offs = matcharray. Index
Pos = I // Save the matching closest to the starting position
}
}
}
If (Pos> = 0 ){
Re. Compile (regexparray [POS], Mode)
VaR matcharray = re.exe C (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 = "\ n not found" + form. reglist. Value
Form. matchlist. value = ""
Nextpoint = 0
Key = false
}
} While (key)
}