ASP regular operation function Encapsulation

Source: Internet
Author: User

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 ")

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.