下面來說講RegExp對象:javascript
Vbs提供了針對正則表達式的一個很是實用的類,就是RegExp Global屬性:表明全局匹配 IgnoreCase屬性:大小寫忽略 Pattern屬性:正則表達式 Execute方法:匹配搜索,返回匹配結果集合 Replace方法:匹配代替,返回替代匹配結果 Test方法:測試匹配,返回布爾類型
下面舉幾個實例:java
'判斷正則匹配是否正確 'msgbox (IsRegMatch("a123","http://www.123.456.com")) Function IsRegMatch(patrn,str) Dim regEx Set regEx = New RegExp regEx.Pattern = patrn regEx.IgnoreCase = False IsRegMatch = regEx.Test(str) Set regEx = nothing End Function '替換匹配字符串 'msgbox (ReplaceRegMatch("9","loader runner 9.0, qtp 9.0","10")) Function ReplaceRegMatch(patrn,str,replaceStr) Dim regEx Set regEx = New RegExp regEx.Pattern = patrn regEx.IgnoreCase = False regEx.Global = True 'false的時候只會替換第一個匹配的字符串。若爲true則會替換全部匹配的字符串 ReplaceRegMatch = regEx.Replace(str,replaceStr) End Function '返回匹配內容 'returnRegMatch "qtp .","qtp 1 qtp 2 qtp3 qtp 4" Function ReturnRegMatch(patrn,str) Dim regEx,matches,match Set regEx = New RegExp regEx.Pattern = patrn regEx.IgnoreCase = true regEx.Global = true '打開全局搜索 Set matches = regEx.Execute(str) For Each match in matches print cstr(match.firstIndex) + " " + match.value + " " + cstr(match.length) Next End Function
最新內容請見做者的GitHub頁:http://qaseven.github.io/git