Regular expressions in VbScript?

Does VbScript have a built-in implementation for Regex? I need to validate email addresses in an old ASP application.

Any pointers would be great.

+1


source to share


5 answers


This example is AlexCuse by LessThanDot



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

      

+8


source


Since the top answer is here in VB6, I thought I'd add it here in VBScript (since then what the question asks about): -

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()

      



Now some tests: -

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

      

+19


source


+1


source


Like the others, yes. I just wanted to put you in the devguru vbscript docs, I think they are generally a good place for a quick vbscript answer. This is a section on the Regexp object .

0


source


VBScript has a built-in RegExp object, which is a JavaScript implementation of JavaScript regular expressions. I have an article on the VBScript RegExp object on my website that explains how to use it.

0


source







All Articles