正则笔记
RegExp对象属性:
1.IgnoreCase
判断搜索是否区分大小写.区分大小写,IgnoreCase属性为 False;否则为 True.
2.Global
判断搜索是否应用于整个字符串,整个搜索,Global属性的值应设为 True.
3.Pattern
设置或返回被搜索的正则表达式模式。
RegExp对象方法:
1.Execute
Match对象以及Matches集合的应用
注:Macth对象的属性
FirstIndex,Length,Value
1 <%
2 Function RegExpTest(patrn, strng)
3 Dim re, Match, Matches
4 Set re = New RegExp
5 re.Pattern = patrn
6 re.IgnoreCase = True
7 re.Global = True
8 Set Matches = re.Execute(strng)
9 For Each Match in Matches
10 RetStr = RetStr & "Match " & I & " found at position "
11 RetStr = RetStr & Match.FirstIndex & ". Match Value is "'
12 RetStr = RetStr & Match.Value
13 Next
14 repTest = RetStr
15 End Function
16 response.write RegExpTest("is.", "IS1 is2 IS3 is4")
17 %>
2 Function RegExpTest(patrn, strng)
3 Dim re, Match, Matches
4 Set re = New RegExp
5 re.Pattern = patrn
6 re.IgnoreCase = True
7 re.Global = True
8 Set Matches = re.Execute(strng)
9 For Each Match in Matches
10 RetStr = RetStr & "Match " & I & " found at position "
11 RetStr = RetStr & Match.FirstIndex & ". Match Value is "'
12 RetStr = RetStr & Match.Value
13 Next
14 repTest = RetStr
15 End Function
16 response.write RegExpTest("is.", "IS1 is2 IS3 is4")
17 %>
SubMatches集合应用
1 <%
2 Function GetStr(inpStr)
3 Dim re,matches,match,gStr
4 Set re = New RegExp
5 re.Global = True 'Gloabl运用
6 re.Pattern = "(\w+)\.(\w+)\.([\.\w]+)"
7 Set matches = re.Execute(inpStr)
8 For Each match in matches
9 gStr = gStr & "网址: " & match & "<br>"
10 gStr = gStr & "服务器:" & match.SubMatches(0) & "<br>"
11 gStr = gStr & "域:" & match. SubMatches(1) & "<br>"
12 gStr = gStr & "机构: " & match. SubMatches(2) & "<br>"
13 Next
14 GetStr = gStr
15 End Function
16 response.write GetStr("网址举例:sports.qq.com,www.sina.com.cn,news.sohu.com")
17 %>
2 Function GetStr(inpStr)
3 Dim re,matches,match,gStr
4 Set re = New RegExp
5 re.Global = True 'Gloabl运用
6 re.Pattern = "(\w+)\.(\w+)\.([\.\w]+)"
7 Set matches = re.Execute(inpStr)
8 For Each match in matches
9 gStr = gStr & "网址: " & match & "<br>"
10 gStr = gStr & "服务器:" & match.SubMatches(0) & "<br>"
11 gStr = gStr & "域:" & match. SubMatches(1) & "<br>"
12 gStr = gStr & "机构: " & match. SubMatches(2) & "<br>"
13 Next
14 GetStr = gStr
15 End Function
16 response.write GetStr("网址举例:sports.qq.com,www.sina.com.cn,news.sohu.com")
17 %>
2.Test
1 <%
2 '检查字符串
3 '返回:True/False
4 Function strTest(patrn,str)
5 dim re
6 set re = new RegExp
7 re.Pattern = patrn
8 re.Global = True
9 strTest = re.test(str)
10 set re = nothing
11 End Function
12 Response.Write strTest("^[_A-Za-z0-9]{5,20}$","asdfasdf")
13 %>
2 '检查字符串
3 '返回:True/False
4 Function strTest(patrn,str)
5 dim re
6 set re = new RegExp
7 re.Pattern = patrn
8 re.Global = True
9 strTest = re.test(str)
10 set re = nothing
11 End Function
12 Response.Write strTest("^[_A-Za-z0-9]{5,20}$","asdfasdf")
13 %>
3.Replace
1 <%
2 '替换字符串
3 Function ReplaceTest(patrn,str,rstr)
4 Dim re
5 Set re = New RegExp
6 re.Pattern = patrn
7 re.IgnoreCase = True
8 ReplaceTest = re.Replace(str, rstr)
9 End Function
10 response.write(ReplaceTest("fox","The quick brown fox jumped over the lazy dog.","cat"))
11 'response.write ReplaceTest("(\S+)(\s+)(\S+)","The quick brown fox jumped over the lazy dog.","$3$2$1")
12 %>
2 '替换字符串
3 Function ReplaceTest(patrn,str,rstr)
4 Dim re
5 Set re = New RegExp
6 re.Pattern = patrn
7 re.IgnoreCase = True
8 ReplaceTest = re.Replace(str, rstr)
9 End Function
10 response.write(ReplaceTest("fox","The quick brown fox jumped over the lazy dog.","cat"))
11 'response.write ReplaceTest("(\S+)(\s+)(\S+)","The quick brown fox jumped over the lazy dog.","$3$2$1")
12 %>