当前位置:  开发笔记 > 后端 > 正文

VbScript中的正则表达式?

如何解决《VbScript中的正则表达式?》经验,为你挑选了2个好方法。

VbScript是否具有Regex的本机实现?我需要在旧的ASP应用程序上验证电子邮件地址.

任何指针都会很棒.



1> AnthonyWJone..:

由于这里的最佳答案是VB6,我想我会在VBScript中添加一个(因为这就是问题所要求的): -

Option Explicit

Function GetEmailValidator()

  Set GetEmailValidator = New RegExp

  GetEmailValidator.Pattern = "^((?:[A-Z0-9_%+-]+\.?)+)@((?:[A-Z0-9-]+\.)+[A-Z]{2,4})$"

  GetEmailValidator.IgnoreCase = True

End Function

Dim EmailValidator : Set EmailValidator = GetEmailValidator()

现在进行一些测试: -

Response.Write EmailValidator.Test("") = False
Response.Write EmailValidator.Test(" ") = False
Response.Write EmailValidator.Test("somebody@domain.co.uk") = True
Response.Write EmailValidator.Test("someone@domain.com") = True
Response.Write EmailValidator.Test("some.body@domain.co.uk") = True
Response.Write EmailValidator.Test("broken@domain..co.uk") = False
Response.Write EmailValidator.Test("@oops.co.uk") = False
Response.Write EmailValidator.Test("name") = False
Response.Write EmailValidator.Test("name@uk") = False
Response.Write EmailValidator.Test("name@uk") = False
Response.Write EmailValidator.Test("name@domain.abcde") = False



2> Fionnuala..:

这个例子来自LessThanDot的AlexCuse

Function ValidEmail(ByVal emailAddress) 

'this function will use regular expressions to check an '
'email address for validity '

'instantiate regex object container, output boolean '
Dim objRegEx, retVal 

'using late binding, vbscript reference is not required '
Set objRegEx = CreateObject("VBScript.RegExp") 

'.pattern -looks for a valid email address '
With objRegEx 
      .Pattern = "^\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$" 
      .IgnoreCase = True 
End With 

retVal = objRegEx.Test(emailAddress) 

'get rid of RegEx object '
Set objRegEx = Nothing 

ValidEmail = retVal 

End Function

推荐阅读
135369一生真爱_890
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有