VbScript是否具有Regex的本机实现?我需要在旧的ASP应用程序上验证电子邮件地址.
任何指针都会很棒.
由于这里的最佳答案是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
这个例子来自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