二、正則表達式在VBScript中的應(yīng)用 VBScript使用RegExp對象、Matches集合以及Match對象提供正則表達式支持功能。我們還是先看一個例子。 % Function RegExpTest(patrn, strng) Dim regEx, Match, Matches '建立變量。 Set regEx = New RegExp '建立正則表達式。 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 " ?,etStr = 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這兩個詞,忽略大小寫。運行的結(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'. 下面我們就介紹這三個對象和集合。 ?,! ?、RegExp對象是最重要的一個對象,它有幾個屬性,其中: ○Global 屬性,設(shè)置或返回一個 Boolean 值,該值指明在整個搜索字符串時模式是全部匹配還是只匹配第一個。如果搜索應(yīng)用于整個字符串,Global 屬性的值為 True,否則其值為 False。默認的設(shè)置為 False。 ○IgnoreCase 屬性,設(shè)置或返回一個Boolean值,指明模式搜索是否區(qū)分大小寫。如果搜索是區(qū)分大小寫的,則 IgnoreCase 屬性為 False;否則為 True。缺省值為 False。 ○Pattern 屬性,設(shè)置或返回被搜索的正則表達式模式。必選項。總是一個 RegExp 對象變量。 ?! ?、Match 對象 匹配搜索的結(jié)果是存放在Match對象中,提供了對正則表達式匹配的只讀屬性的訪問。 Match 對象只能通過 RegExp 對象的 Execute 方法來創(chuàng)建,該方法實際上返回了 Match 對象的集合。所有的 Match 對象屬性都是只讀的。在執(zhí)行正則表達式時,可能產(chǎn)生零個或多個 Match 對象。每個 Match 對象提供了被正則表達式搜索找到的字符串的訪問、字符串的長度,以及找到匹配的索引位置等。 ○FirstIndex 屬性,返回在搜索字符串中匹配的位置。FirstIndex 屬性使用從零起算的偏移量,該偏移量是相對于搜索字符串的起始位置而言的。換言之,字符串中的第一個字符被標識為字符 0 ○Length 屬性,返回在字符串搜索中找到的匹配的長度。 ○value 屬性,返回在一個搜索字符串中找到的匹配的值或文本。 3、Matches 集合 正則表達式 Match 對象的集合。Matches 集合中包含若干獨立的 Match 對象,只能使用 RegExp 對象的 Execute 方法來創(chuàng)建之。與獨立的 Match 對象屬性相同,Matches `集合的一個屬性是只讀的。在執(zhí)行正則表達式時,可能產(chǎn)生零個或多個 Match 對象。每個 Match 對象都提供了與正則表達式匹配的字符串的訪問入口、字符串的長度,以及標識匹配位置的索引。 學(xué)習了這三個對象和集合,如何應(yīng)用于字符串的判斷和替換呢?regExp對象的三個方法正好解決了這個問題,它們是Replace方法、Test方法和Execute方法。 1、Replace 方法 替換在正則表達式查找中找到的文本。我們還是先看個例子:下面的例子說明了 Replace 方法的用法。 % Function ReplaceTest(patrn, replStr) Dim regEx, str1?,,,,,,,,,,,,,,,,,? 建立變量。 str1 = "The quick brown fox jumped over the lazy dog." Set regEx = New RegExp?,,,,,,,,,? 建立正則表達式。 regEx.Pattern = patrn?,,,,,,,,,,,,,?nbsp;' 設(shè)置模式。 regEx.IgnoreCase = True?,,,,,,,,,,? 設(shè)置是否區(qū)分大小寫。 ReplaceTest = regEx.Replace(str1, replStr)?,? 作替換。 End Function Response.write ReplaceTest("fox", "cat") "BR>"?,,,,,? 將 'fox' 替換為 'cat'。 Response.write ReplaceTest("(\S+)(\s+)(\S+)", "$3$2$1")?,,,? 交換詞對. %> 2、Test 方法 對指定的字符串執(zhí)行一個正則表達式搜索,并返回一個 Boolean 值指示是否找到匹配的模式。正則表達式搜索的實際模式是通過RegExp對象的Pattern屬性來設(shè)置的。RegExp.Global屬性對Test方法沒有影響。 如果找到了匹配的模式,Test方法返回True;否則返回False。下面的代碼說明了Test 方法的用法。 % Function RegExpTest(patrn, strng) Dim regEx, retVal?,,,,,,,?nbsp;' 建立變量。 Set regEx = New RegExp?,,,?nbsp;' 建立正則表達式。 regEx.Pattern = patrn?,,,,,,?nbsp;' 設(shè)置模式。 regEx.IgnoreCase = False?,,,?nbsp;' 設(shè)置是否區(qū)分大小寫。 retVal = regEx.Test(strng)?,,,?nbsp;' 執(zhí)行搜索測試。 If retVal Then ?,egExpTest = "找到一個或多個匹配。" Else ?,egExpTest = "未找到匹配。" End If End Function Response.write RegExpTest("is.", "IS1 is2 IS3 is4") %> 3、Execute 方法 對指定的字符串執(zhí)行正則表達式搜索。正則表達式搜索的設(shè)計模式是通過 RegExp 對象的 Pattern 來設(shè)置的。 Execute 方法返回一個 Matches 集合,其中包含了在 string 中找到的每一個匹配的 Match 對象。如果未找到匹配,Execute 將返回空的 Matches 集合。
三、javascript中正則表達式的使用 在javascript 1.2版以后,javascript也支持正則表達式。 1、replace replace在一個字符串中通過正則表達式查找替換相應(yīng)的內(nèi)容。replace并不改變原來的字符串,只是重新生成了一個新的字符串。如果需要執(zhí)行全局查找或忽略大小寫,那么在正則表達式的最后添加g和i。 例: SCRIPT> re = /apples/gi; str = "Apples are round, and apples are juicy."; newstr=str.replace(re, "oranges"); document.write(newstr) /SCRIPT> 結(jié)果是:"oranges are round, and oranges are juicy." 例: SCRIPT> str = "Twas the night before Xmas..."; newstr=str.replace(/xmas/i, "Christmas"); document.write(newstr) /SCRIPT> 結(jié)果是:"Twas the night before Christmas..." 例: SCRIPT> re = /(\w+)\s(\w+)/;str = "John Smith"; newstr = str.replace(re, "$2, $1"); document.write(newstr) /SCRIPT> 結(jié)果是:"Smith, John". 2、search ?,earch通過正則表達式查找相應(yīng)的字符串,只是判斷有無匹配的字符串。如果查找成功,search返回匹配串的位置,否則返回-1。 search(regexp) SCRIPT> function testinput(re, str){ if (str.search(re) != -1) ?,,idstring = " contains "; else ?,,?nbsp;midstring = " does not contain "; document.write (str + midstring + re.source); } testinput(/^[1-9]/i,"123") /SCRIPT> 3、match match方法執(zhí)行全局查找,查找結(jié)果存放在一個數(shù)組里。 例一: SCRIPT> str = "For more information, see Chapter 3.4.5.1"; re = /(chapter \d+(\.\d)*)/i; found = str.match(re); document.write(found); /SCRIPT> 顯示結(jié)果:Chapter 3.4.5.1,Chapter 3.4.5.1,.1 例二: SCRIPT> str = "abcDdcba"; newArray = str.match(/d/gi); document.write(newArray); /SCRIPT> 顯示結(jié)果D, d.