VBScript - Regular Expressions

Regular Expressions is a sequence of characters that forms a pattern, which is mainly used for search and replace. The purpose of creating a pattern is to match specific strings, so that the developer can extract characters based on conditions and replace certain characters.
RegExp object helps the developers to match the pattern of strings and the properties and methods help us to work with Regular Expressions easily. It is similar to RegExp in JavaScript
Pattern − The Pattern method represents a string that is used to define the regular expression and it should be set before using the regular expression object.
Pattern − The Pattern method represents a string that is used to define the regular expression and it should be set before using the regular expression object.
IgnoreCase − A Boolean property that represents if the regular expression should be tested against all possible matches in a string if true or false. If not specified explicitly, IgnoreCase value is set to False.
IgnoreCase − A Boolean property that represents if the regular expression should be tested against all possible matches in a string if true or false. If not specified explicitly, IgnoreCase value is set to False.
Global − A Boolean property that represents if the regular expression should be tested against all possible matches in a string. If not specified explicitly, Global value is set to False.
Global − A Boolean property that represents if the regular expression should be tested against all possible matches in a string. If not specified explicitly, Global value is set to False.
Test(search-string) − The Test method takes a string as its argument and returns True if the regular expression can successfully be matched against the string, otherwise False is returned.
Test(search-string) − The Test method takes a string as its argument and returns True if the regular expression can successfully be matched against the string, otherwise False is returned.
Replace(search-string, replace-string) − The Replace method takes 2 parameters. If the search is successful then it replaces that match with the replace-string, and the new string is returned. If there are no matches then the original search-string is returned.
Replace(search-string, replace-string) − The Replace method takes 2 parameters. If the search is successful then it replaces that match with the replace-string, and the new string is returned. If there are no matches then the original search-string is returned.
Execute(search-string) − The Execute method works like Replace, except that it returns a Matches collection object, containing a Match object for each successful match. It doesn't modify the original string.
Execute(search-string) − The Execute method works like Replace, except that it returns a Matches collection object, containing a Match object for each successful match. It doesn't modify the original string.
The Matches collection object is returned as a result of the Execute method. This collection object can contain zero or more Match objects and the properties of this object are read-only.
Count − The Count method represents the number of match objects in the collection.
Count − The Count method represents the number of match objects in the collection.
Item − The Item method enables the match objects to be accessed from matches collections object.
Item − The Item method enables the match objects to be accessed from matches collections object.
The Match object is contained within the matches collection object. These objects represent the successful match after the search for a string.
FirstIndex − It represents the position within the original string where the match occurred. This index are zero-based which means that the first position in a string is 0.
FirstIndex − It represents the position within the original string where the match occurred. This index are zero-based which means that the first position in a string is 0.
Length − A value that represents the total length of the matched string.
Length − A value that represents the total length of the matched string.
Value − A value that represents the matched value or text. It is also the default value when accessing the Match object.
Value − A value that represents the matched value or text. It is also the default value when accessing the Match object.
The pattern building is similar to PERL. Pattern building is the most important thing while working with Regular Expressions. In this section, we will deal with how to create a pattern based on various factors.
The significance of position matching is to ensure that we place the regular expressions at the correct places.
Any form of characters such as alphabet, number or special character or even decimal, hexadecimal can be treated as a Literal. Since few of the characters have already got a special meaning within the context of Regular Expression, we need to escape them using escape sequences.
The character classes are the Pattern formed by customized grouping and enclosed within [ ] braces. If we are expecting a character class that should not be in the list, then we should ignore that particular character class using the negative symbol, which is a cap ^.
Repetition matching allows multiple searches within the regular expression. It also specifies the number of times an element is repeated in a Regular Expression.
Alternation and grouping helps developers to create more complex Regular Expressions in particularly handling intricate clauses within a Regular Expression which gives a great flexibility and control.
Given below are a few examples that clearly explain how to build a Regular Expression.
The below example checks whether or not the user entered an email id whose format should match such that there is an email id followed by '@' and then followed by domain name.
<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         strid = "welcome.user@tutorialspoint.co.us"
         Set re = New RegExp
         With re
            .Pattern    = "^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$"
            .IgnoreCase = False
            .Global     = False
         End With
        
         ' Test method returns TRUE if a match is found
         If re.Test( strid ) Then
            Document.write(strid & " is a valid e-mail address")
         Else
            Document.write(strid & " is NOT a valid e-mail address")
         End If
        
        Set re = Nothing
      </script>
   </body>
</html>

    63 Lectures 
     4 hours 

 Frahaan Hussain
 Print
 Add Notes
 Bookmark this page