Regular Expressions can also be used in ASP. For ease of use, regular expression operations are encapsulated. Currently, only two operations are encapsulated:
1. Regular Expression replacement
'Function: replace a string with a regular expression.
'Parameter: Str original string
'Patton Regular Expression
'Restr replacement string
'Op option contains I case-insensitive,
'G indicates global match. General Usage: "ig"
'Return: the string after replacement
'Example: regreplace ("ss5ss6s7s", "\ D", "", "ig") returns Ssssss
Public Function regreplace (STR, Patton, restr, OP)
Dim RegEx: Set RegEx = new Regexp
If instr (OP, "I")> 0 then
RegEx. ignorecase = true
End if
If instr (OP, "G")> 0 then
RegEx. Global = true
End if
RegEx. pattern = Patton
Regreplace = RegEx. Replace (STR, restr)
Set RegEx = nothing
End Function
Example: Remove all HTML tags from a string.
STR = "dddffffdddd <SCRIPT> Ssssss </SCRIPT> <HTML> Response. Write regreplace (STR, "<(. *). +>. * <\/\ 1>", "", "ig ")
2. Regular Expression Detection
'Function: checks whether the expression contains a string using a regular expression.
'Parameter: Str original string
'Patton Regular Expression
'Op option contains I case-insensitive,
'G indicates global match. General Usage: "ig"
'Return: whether a match exists
'Example: regreplace ("ss5ss6s7s", "\ D", "ig") returns true
Public Function regtest (STR, Patton, OP)
Dim RegEx: Set RegEx = new Regexp
If instr (OP, "I")> 0 then
RegEx. ignorecase = true
End if
If instr (OP, "G")> 0 then
RegEx. Global = true
End if
RegEx. pattern = Patton
Regtest = RegEx. Test (STR)
Set RegEx = nothing
End Function
Example: Check whether an HTML string exists in a string
STR = "dddffffdddd <SCRIPT> Ssssss </SCRIPT> <HTML> Response. Write regreplace (STR, "<(. *). +>. * <\/\ 1>", "ig ")