VbScript是否具有Regex的本机实现?我需要在旧的ASP应用程序上验证电子邮件地址。
任何指针都会很棒。
答案 0 :(得分:19)
由于这里的最佳答案是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
答案 1 :(得分:8)
这个例子来自LessThanDot
的AlexCuseFunction 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
答案 2 :(得分:1)
是的,确实如此。 Here's Microsoft's documention
答案 3 :(得分:0)
答案 4 :(得分:0)
VBScript有一个内置的RegExp对象,它是Microsoft的JavaScript正则表达式的实现。我的网站上有一个article about the VBScript RegExp object,解释了如何使用它。