摘要: 下面来讲讲RegExp对象: Vbs提供了针对正则表达式的一个非常实用的类,就是RegExpGlobal属性:代表全局匹配IgnoreCase属性:大小写忽略Pattern属性:正则表达式Execute方法:匹配搜索,返回匹配结果集合Repla ...
下面来讲讲RegExp对象: Vbs提供了针对正则表达式的一个非常实用的类,就是RegExp |
Global属性:代表全局匹配IgnoreCase属性:大小写忽略Pattern属性:正则表达式Execute方法:匹配搜索,返回匹配结果集合Replace方法:匹配代替,返回替代匹配结果Test方法:测试匹配,返回布尔类型 下面举几个实例:'判断正则匹配是否正确'msgbox (IsRegMatch("a123","http://www.123.456.com"))Function IsRegMatch(patrn,str)Dim regExSet regEx = New RegExpregEx.Pattern = patrnregEx.IgnoreCase = FalseIsRegMatch = regEx.Test(str)Set regEx = nothingEnd Function'替换匹配字符串'msgbox (ReplaceRegMatch("9","loader runner 9.0, qtp 9.0","10"))Function ReplaceRegMatch(patrn,str,replaceStr)Dim regExSet regEx = New RegExpregEx.Pattern = patrnregEx.IgnoreCase = FalseregEx.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,matchSet regEx = New RegExpregEx.Pattern = patrnregEx.IgnoreCase = trueregEx.Global = true '打开全局搜索Set matches = regEx.Execute(str)For Each match in matchesprint cstr(match.firstIndex) + " " + match.value + " " + cstr(match.length)NextEnd Function