欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP 正則表達(dá)式常用的幾種方法(execute、test、replace)

 更新時(shí)間:2009年12月14日 03:08:34   作者:  
asp下正則表達(dá)式常用的幾種方法,需要的朋友可以參考下。
RegExp就是建立正則的對像。
如:
Set regEx = New RegExp

regEx.Pattern 就是來設(shè)置正則的模式的,
如:
regEx.Pattern ="/d+"

regEx.IgnoreCase = True ' 設(shè)置是否區(qū)分大小寫
regEx.Global = True ' 設(shè)置全程可用性。


RegExp對像有3種方法,分別是execute、test、replace。

test方法是對指定的字符串執(zhí)行一個(gè)正則表達(dá)式搜索,并返回一個(gè) Boolean 值指示是否找到匹配的模式。RegExp.Global屬性對Test方法沒有影響。如果找到了匹配的模式,Test方法返回True;否則返回False。
例子:

測試的時(shí)候,msgbox是vbs的用法,如果是asp文件,需要將msgbox替換為response.write
復(fù)制代碼 代碼如下:

Function RegExpTest(patrn, strng)
Dim regEx, retVal ' 建立變量。
Set regEx = New RegExp ' 建立正則表達(dá)式。
regEx.Pattern = patrn ' 設(shè)置模式。
regEx.IgnoreCase = False ' 設(shè)置是否區(qū)分大小寫。
retVal = regEx.Test(strng) ' 執(zhí)行搜索測試。
If retVal Then
RegExpTest = "找到一個(gè)或多個(gè)匹配。"
Else
RegExpTest = "未找到匹配。"
End If
End Function

MsgBox(RegExpTest("\d+", "abcd1234"))
MsgBox(RegExpTest("\d+", "abcd"))


Replace 方法替換在正則表達(dá)式查找中找到的文本
例子:
vbs代碼
復(fù)制代碼 代碼如下:

Function ReplaceTest(str,patrn, replStr)
Dim regEx, str1 ' 建立變量。
'str1 = "dog 123."
Set regEx = New RegExp ' 建立正則表達(dá)式。
regEx.Pattern = patrn ' 設(shè)置模式。
regEx.IgnoreCase = True ' 設(shè)置是否區(qū)分大小寫。
ReplaceTest = regEx.Replace(str, replStr) ' 作替換。
End Function

MsgBox(ReplaceTest("dog 123","\d+", "cat")) '將字符串中的123替換為cat


Execute 方法,則是對指定的字符串執(zhí)行正則表達(dá)式搜索。這里又涉及到Match對像和Matches 集合。Matches 集合就是match的對像集合。Matches 集合中包含若干獨(dú)立的 Match 對象,只能使用 RegExp 對象的 Execute 方法來創(chuàng)建之。例子:
vbs測試代碼
復(fù)制代碼 代碼如下:

Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches ' 建立變量。
Set regEx = New RegExp ' 建立正則表達(dá)式。
regEx.Pattern = patrn ' 設(shè)置模式。
regEx.IgnoreCase = True ' 設(shè)置是否區(qū)分大小寫。
regEx.Global = True ' 設(shè)置全程可用性。
Set Matches = regEx.Execute(strng) ' 執(zhí)行搜索。
For Each Match in Matches ' 遍歷 Matches 集合。
RetStr = RetStr & Match.FirstIndex & "。匹配的長度為"&" "
RetStr = RetStr & Match.Length &" "
RetStr = RetStr & Matches(0) &" " '值為123
RetStr = RetStr & Matches(1)&" " '值為44
RetStr = RetStr & Match.value&" " '值為123和44的數(shù)組
RetStr = RetStr & vbCRLF
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest("\d+", "123a44"))

相關(guān)文章

最新評論