<% 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'遍歷匹配集合。 RetStr = RetStr & "Match found at position " RetStr = RetStr & Match.FirstIndex & ". Match Value is '" RetStr = RetStr & Match.Value & "'." & "<BR>" Next RegExpTest = RetStr End Function
response.Write RegExpTest("[ij]s.", "IS1 Js2 IS3 is4") %> 在這個例子中,我們查找字符串中有無is或者js這兩個詞,忽略大小寫。運(yùn)行的結(jié)果如下: Match found at position 0. Match Value is 'IS1'. Match found at position 4. Match Value is 'Js2'. Match found at position 8. Match Value is 'IS3'. Match found at position 12. Match Value is 'is4'. 下面我們就介紹這三個對象和集合。 1、RegExp對象是最重要的一個對象,它有幾個屬性,其中: ○Global 屬性,設(shè)置或返回一個 Boolean 值,該值指明在整個搜索字符串時模式是全部匹配還是只匹配第一個。如果搜索應(yīng)用于整個字符串,Global 屬性的值為 True,否則其值為 False。默認(rèn)的設(shè)置為 False。 ○IgnoreCase 屬性,設(shè)置或返回一個Boolean值,指明模式搜索是否區(qū)分大小寫。如果搜索是區(qū)分大小寫的,則 IgnoreCase 屬性為 False;否則為 True。缺省值為 False。 ○Pattern 屬性,設(shè)置或返回被搜索的正則表達(dá)式模式。必選項(xiàng)。總是一個 RegExp 對象變量。 2、Match 對象 匹配搜索的結(jié)果是存放在Match對象中,提供了對正則表達(dá)式匹配的只讀屬性的訪問。 Match 對象只能通過 RegExp 對象的 Execute 方法來創(chuàng)建,該方法實(shí)際上返回了 Match 對象的集合。所有的 Match 對象屬性都是只讀的。在執(zhí)行正則表達(dá)式時,可能產(chǎn)生零個或多個 Match 對象。每個 Match 對象提供了被正則表達(dá)式搜索找到的字符串的訪問、字符串的長度,以及找到匹配的索引位置等。