Several common methods of ASP regular expression (execute, test, replace)

Time:2021-5-8

Regexp is to create regular objects.
For example:
Set regEx = New RegExp

Regex. Pattern is used to set regular patterns,
For example:
regEx.Pattern =”/d+”

Regex. Ignorecase = true ‘sets whether case sensitive
Regex. Global = true ‘sets 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 a matching pattern is found. The regexp.global property has no effect on the test method. If a matching pattern is found, the test method returns true; Otherwise, it returns false.
example:

When testing, msgbox is the usage of VBS. If it is an ASP file, you need to replace msgbox withresponse.write

Copy codeThe code is as follows:
Function RegExpTest(patrn, strng)
Dim regex, retval’create variables.
Set regex = new regexp’creates a regular expression.
Regex. Pattern = pattern ‘setting mode.
Regex. Ignorecase = false ‘sets whether case sensitive.
Retval = regex. Test (strng) ‘performs the search test.
If retVal Then
Regexptest = “one or more matches found.”
Else
Regexptest = no match found
End If
End Function

MsgBox(RegExpTest(“\d+”, “abcd1234”))
MsgBox(RegExpTest(“\d+”, “abcd”))

The replace method replaces the text found in the regular expression lookup
example:
VBS code

Copy codeThe code is as follows:
Function ReplaceTest(str,patrn, replStr)
Dim regex, str1’to create variables.
‘str1 = “dog 123.”
Set regex = new regexp’creates a regular expression.
Regex. Pattern = pattern ‘setting mode.
Regex. Ignorecase = true ‘sets whether case sensitive.
Replacetest = regex. Replace (STR, replacestr) ‘.
End Function

Msgbox (replacetest (“dog 123”, “D +”, “cat”)) ‘replaces 123 in the string with cat

The execute method performs a regular expression search on the specified string. Here we deal with the match object and the matches set. The matches set is the object set of match. The matches collection contains several independent match objects, which can only be created using the execute method of the regexp object. example:
VBS test code

Copy codeThe code is as follows:
Function RegExpTest(patrn, strng)
Dim regex, match, matches’ create variables.
Set regex = new regexp’creates a regular expression.
Regex. Pattern = pattern ‘setting mode.
Regex. Ignorecase = true ‘sets whether case sensitive.
Regex. Global = true ‘sets full availability.
Set matches = regex. Execute (strng) ‘to perform the search.
For each match in matches’ iterates through the matches set.
RetStr = RetStr & Match.FirstIndex & “。 The length of the match is “&”“
RetStr = RetStr & Match.Length &” ”
Retstr = retstr & matches (0) & ”’value is 123
Retstr = retstr & matches (1) & ”’value is 44
Retstr = retstr & match. Value & “‘An array of values 123 and 44
RetStr = RetStr & vbCRLF
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest(“\d+”, “123a44”))