Several methods commonly used in ASP regular expressions (execute, test, replace) _ Regular expressions

Source: Internet
Author: User
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") )

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.