首页前端开发其他前端知识VBS中的正则表达式的用法大全 <font color=red>原创</font>

VBS中的正则表达式的用法大全 <font color=red>原创</font>

时间2024-02-01 11:35:03发布访客分类其他前端知识浏览449
导读:收集整理的这篇文章主要介绍了VBS中的正则表达式的用法大全 <font color=red>原创</font>,觉得挺不错的,现在分享给大家,也给大家做个参考。...
收集整理的这篇文章主要介绍了VBS中的正则表达式的用法大全 font color=red> 原创/font> ,觉得挺不错的,现在分享给大家,也给大家做个参考。 vbS正则表达式函数
主要用在asp中效果明显 
1、表单验证功能
复制代码 代码如下: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
测试 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
[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]
2、替换功能
复制代码 代码如下:
'==========================
'用正则表达式实现替换
'==========================
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

3、ubb功能
https://www.js-code.com/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。

例子:

复制代码 代码如下:
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 方法来创建之。例子:

复制代码 代码如下:
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"))

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

VBS

若转载请注明出处: VBS中的正则表达式的用法大全 &#60;font color=red&#62;原创&#60;/font&#62;
本文地址: https://pptw.com/jishu/595347.html
正则表达式详述 四 [PHP]常用正则表达式收集

游客 回复需填写必要信息