RegExp is to establish a regular pair of image.
Such as:
Set regEx = New RegExp
Regex.pattern is the way to set the regular pattern,
Such as:
Regex.pattern = "/d+"
Regex.ignorecase = True ' Sets whether case sensitive
Regex.global = True ' Sets full availability.
There are 3 ways to RegExp, namely execute, test, and replace.
The test method performs a regular expression search on the specified string and returns a Boolean indicating whether the matching pattern is found. The Regexp.global property has no effect on the test method. The test method returns true if a matching pattern is found, otherwise it returns false.
Example:
When testing, MsgBox is the use of VBS, if it is an ASP file, you need to replace MsgBox withResponse.Write
Copy Code code as follows:
Function regexptest (PATRN, STRNG)
Dim regEx, RetVal ' Set variable.
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set mode.
Regex.ignorecase = False ' Sets whether case sensitive.
RetVal = regex.test (strng) ' performs a search test.
If RetVal Then
Regexptest = "Find one or more matches. "
Else
Regexptest = "no match found. "
End If
End Function
MsgBox (Regexptest ("\d+", "abcd1234"))
MsgBox (Regexptest ("\d+", "ABCD"))
Replace method replaces text found in a regular expression lookup
Example:
VBS code
Copy Code code as follows:
Function replacetest (STR,PATRN, REPLSTR)
Dim regEx, str1 ' Set variable.
' str1 = ' dog 123. '
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set mode.
Regex.ignorecase = True ' Sets whether case sensitive.
Replacetest = Regex.Replace (str, REPLSTR) ' for replacement.
End Function
MsgBox (Replacetest ("Dog 123", "\d+", "Cat")) ' replaces 123 of the string with cat
Execute method, a regular expression search is performed on the specified string. This also involves match pairs and matches collections. The matches set is the set of match pairs. The Matches collection contains several separate Match objects that can only be created using the Execute method of the RegExp object. Example:
VBS TEST Code
Copy Code code as follows:
Function regexptest (patrn, strng)
Dim regEx, Match, matches ' create variable. The
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set mode. The
Regex.ignorecase = True ' setting is case-sensitive.
Regex.global = True ' Sets full availability.
Set matches = Regex.execute (strng) ' performs a search. The
for all Match in matches ' traverses the matches collection.
Retstr = retstr & Match.firstindex &. The matching length is "&"
Retstr = retstr & Match.length & ""
Retstr = retstr & Matches (0) & "" ' Value is 123
Retstr = retstr & Matches (1) & "" ' Value is 44
Retstr = retstr & match.value& "" ' array of values 123 and
Ret STR = retstr & VbCRLF
Next
regexptest = retstr
End Function
MsgBox (regexptest ("\d+", "123a44") )