Regexp is used to create a regular expression.
For example:
Set RegEx = new Regexp
RegEx. pattern is used to set the regular expression mode,
For example:
RegEx. pattern = "/d +"
RegEx. ignorecase = true' specifies whether the value is case sensitive.
RegEx. Global = true' to set full availability.
Regexp has three methods: Execute, test, and replace.
The test method performs a regular expression search on the specified string and returns a Boolean value indicating whether the matching mode is found. The Regexp. Global attribute has no effect on the test method. If the matching mode is found, the test method returns true; otherwise, false.
Example:
During the test, msgbox is used by vbs. If it is an ASP file, replace msgbox with response. Write
Copy the Code as follows:
Function regexptest (patrn, strng)
Dim RegEx, retval 'to create a variable.
Set RegEx = new Regexp 'to create a regular expression.
RegEx. pattern = patrn 'setting mode.
RegEx. ignorecase = false' specifies whether to enable case sensitivity.
Retval = RegEx. Test (strng.
If retval then
Regexptest = "one or more matches are found. "
Else
Regexptest = "no matching found. "
End if
End Function
Msgbox (regexptest ("/d +", "abcd1234 "))
Msgbox (regexptest ("/d +", "ABCD "))
Replace method replaces the text found in regular expression search
Example:
Vbs code
Copy the Code as follows:
Function replacetest (STR, patrn, replstr)
Dim RegEx, str1' create a variable.
'Str1 = "Dog 123 ."
Set RegEx = new Regexp 'to create a regular expression.
RegEx. pattern = patrn 'setting mode.
RegEx. ignorecase = true' is used to set Case sensitivity.
Replacetest = RegEx. Replace (STR, replstr) 'to replace.
End Function
Msgbox (replacetest ("Dog 123", "/d +", "cat") 'replaces 123 in the string with Cat
Execute method is to execute Regular Expression search on the specified string. The match object and matches set are involved here. The matches set is the set of matching objects. The matches set contains several independent match objects, which can only be created using the execute method of the Regexp object. Example:
The Code is as follows:
Function regexptest (patrn, strng)
Dim RegEx, match, matches 'to create a variable.
Set RegEx = new Regexp 'to create a regular expression.
RegEx. pattern = patrn 'setting mode.
RegEx. ignorecase = true' is used to set Case sensitivity.
RegEx. Global = true' to set full availability.
Set matches = RegEx. Execute (strng) 'to execute the search.
For each match in matches 'traverses the matches set.
Retstr = retstr & Match. firstindex &". The matching length is "&""
Retstr = retstr & Match. Length &""
Retstr = retstr & matches (0) & "" 'value: 123
Retstr = retstr & matches (1) & "" 'value: 44
Retstr = retstr & Match. Value & "" 'array with values of 123 and 44
Retstr = retstr & vbcrlf
Next
Regexptest = retstr
End Function
Msgbox (regexptest ("/d +", "123a44 "))