VBS正则表达式RegExp对象用法详解
VBScript中正则表达式的核心在于RegExp对象的使用。
该技术在ASP开发场景中表现尤为突出。
一、实现表单数据验证功能
以下代码演示了如何封装验证函数:Function Validate(strng,patrn)
首先声明变量:Dim regEx
实例化正则对象:Set regEx = New RegExp
设定匹配模式:regEx.Pattern = patrn
忽略大小写差异:regEx.IgnoreCase = True
启用全局搜索:regEx.Global = True
执行测试并返回结果:Validate = regEx.test(strng)
释放对象资源:Set regEx = Nothing
结束函数定义:End Function
二、实际应用场景示例
例如判断文件夹名称是否符合特定格式:If Validate(Fdr.Name,"F\d{4}_P\d{4}")=True Then
... ...
满足条件则执行后续逻辑:End If
<input name=jb51 value="13003523068"> <button onclick=checkit(jb51.value)>测试</button> <script language=vbscript> function check(str) dim re set re=new regexp re.global=true re.ignorecase=true re.pattern="1[3|5]\d{9}" check=re.test(str) set re=nothing end function sub checkit(str) if check(str)=true then msgbox "是手机" else msgbox "不是手机" end if end sub </script>[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]
三、利用正则进行文本替换
相关实现代码如下:
'==========================
'通过正则表达式完成字符串替换操作
'==========================
function replaceregex(patern,str,tagstr)
定义所需变量:dim regex,matches
创建正则实例:set regex=new regExp
配置匹配模式:regex.pattern=patern
开启忽略大小写:regex.IgnoreCase=true
设置全局匹配:regex.global=true
执行替换操作:matches=regex.replace(str,tagstr)
返回替换后的结果:replaceregex=matches
结束函数:end function
四、其他高级功能拓展
https://www.jb51.net/html/200608/6/574.htm
以下是 作者:LCX的内容
以前一直没有好好的学过,这次整理一下。
正则中/d+就是代表一个或多个数字,用这个做例子。
RegExp就是建立正则的对像。如Set regEx = New RegExp。regEx.Pattern 就是来设置正则的模式的,如
regEx.Pattern ="/d+"。regEx.IgnoreCase = True ' 设置是否区分大小写。regEx.Global = True ' 设置全程可用性。
RegExp对像有3种方法,分别是execute、test、replace。
test方法是对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。RegExp.Global属性对Test方法没有影响。如果找到了匹配的模式,Test方法返回True;否则返回False。
五、Test方法代码示例
复制代码 代码如下: Function RegExpTest(patrn, strng) Dim regEx, retVal ' 建立变量。 Set regEx = New RegExp ' 建立正则表达式。 regEx.Pattern = patrn ' 设置模式。 regEx.IgnoreCase = False ' 设置是否区分大小写。 retVal = regEx.Test(strng) ' 执行搜索测试。 If retVal Then RegExpTest = "找到一个或多个匹配。" Else RegExpTest = "未找到匹配。" End If End Function MsgBox(RegExpTest("\d+", "abcd1234")) MsgBox(RegExpTest("\d+", "abcd"))
六、Replace方法代码示例
复制代码 代码如下: Function ReplaceTest(patrn, replStr) Dim regEx, str1 ' 建立变量。 str1 = "dog 123." Set regEx = New RegExp ' 建立正则表达式。 regEx.Pattern = patrn ' 设置模式。 regEx.IgnoreCase = True ' 设置是否区分大小写。 ReplaceTest = regEx.Replace(str1, replStr) ' 作替换。 End Function MsgBox(ReplaceTest("\d+", "cat")) ‘将字符串中的123替换为cat
Execute 方法,则是对指定的字符串执行正则表达式搜索。这里又涉及到Match对像和Matches 集合。Matches 集合就是match的对像集合。Matches 集合中包含若干独立的 Match 对象,只能使用 RegExp 对象的 Execute 方法来创建之。例子: Dim re, m, ms Set re = New RegExp re.Pattern = "\d+" re.Global = True Set ms = re.Execute("abc123def456") For Each m In ms WScript.Echo m.Value Next
复制代码 代码如下: Function RegExpTest(patrn, strng) Dim regEx, Match, Matches ' 建立变量。 Set regEx = New RegExp ' 建立正则表达式。 regEx.Pattern = patrn ' 设置模式。 regEx.IgnoreCase = True ' 设置是否区分大小写。 regEx.Global = True ' 设置全程可用性。 Set Matches = regEx.Execute(strng) ' 执行搜索。 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的数组 RetStr = RetStr & vbCRLF Next RegExpTest = RetStr End Function MsgBox(RegExpTest("\d+", "123a44"))