<%
Function regexptest (PATRN, STRNG)
Dim regEx, Match, matches ' Set variable.
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set mode.
Regex.ignorecase = True ' Sets whether character case is case-sensitive.
Regex.global = True ' Sets global availability.
Set matches = Regex.execute (strng) ' performs a search.
For the match in matches ' traversal matching collection.
Retstr = retstr & "Match found at position"
Retstr = retstr & Match.firstindex & ". Match Value is ' "
Retstr = retstr & Match.value & "'." & "<BR>"
Next
Regexptest = Retstr
End Function
Response. Write regexptest ("[Ij]s.", "IS1 Js2 IS3 is4")
%>
In this example, we look for two words in the string that have no is or JS, ignoring the case. The results of the operation are as follows:
Match found at position 0. Match Value is ' IS1 '.
Match found at position 4. Match Value is ' Js2 '.
Match found at position 8. Match Value is ' IS3 '.
Match found at position 12. Match Value is ' is4 '.
Let's introduce the three objects and collections below.
1, the RegExp object is the most important object, it has several attributes, including:
The 0Global property, sets or returns a Boolean value that indicates whether the pattern matches all or only the first of the entire search string. If the search applies to the entire string, the value of the Global property is True, otherwise its value is False. The default setting is False.
The 0IgnoreCase property, sets or returns a Boolean value that indicates whether the pattern search is case-sensitive. If the search is case-sensitive, the IgnoreCase property is False; otherwise, true. The default value is False.
The 0Pattern property, sets or returns the regular expression pattern that is searched. Required option. Always a REGEXP object variable.
2, Match Object
The result of a matching search is stored in the match object, providing access to the read-only property that matches the regular expression. The match object can only be created by using the RegExp object's Execute method, which actually returns a collection of Match objects. All of the Match object properties are read-only. When you execute a regular expression, you may produce 0 or more Match objects. Each match object provides access to the string found by the regular expression search, the length of the string, and the location of the matching index.
0FirstIndex property that returns the location of the match in the search string. The FirstIndex property uses an offset from zero, which is relative to the starting position of the search string. In other words, the first character in the string is identified as a character 0
The 0Length property, which returns the length of the match found in the string search.
A 0Value property that returns the matching value or text found in a search string.
3, Matches Collection
The collection of regular Expression Match objects. The Matches collection contains several separate Match objects that can only be created using the Execute method of the RegExp object. As with the independent Match object properties, one of the properties of the matches ' collection is read-only. When you execute a regular expression, you may produce 0 or more Match objects. Each match object provides an access entry to the string that matches the regular expression, the length of the string, and an index that identifies the matching location.
Learning about these three objects and collections, how do you apply them to the judgment and substitution of strings? The three methods of the RegExp object solve the problem, they are the Replace method, the test method, and the Execute method.
1. Replace method
Replaces the text found in the regular expression lookup. Let's take a look at an example: The following example illustrates the use of the Replace method.
<%
Function replacetest (PATRN, REPLSTR)
Dim regEx, str1 ' Set variable.
STR1 = "The quick brown fox jumped over the lazy dog."
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set mode.
Regex.ignorecase = True ' Sets whether case sensitive.
Replacetest = Regex.Replace (str1, Replstr) ' for replacement.
End Function
Response.Write Replacetest ("Fox", "Cat") & "<BR>" Replaces ' Fox ' with ' cat '.
Response.Write Replacetest ("(s+) (s+) (s+)", "$3$2$1") ' exchange of words.
%>
2. Test method
Performs a regular expression search on the specified string and returns a Boolean indicating whether the matching pattern is found. The actual mode of the regular expression search is set by regexp the Pattern property of the object. 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. The following code illustrates the use of the test method.
<%
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
Response.Write Regexptest ("is.", "IS1 is2 IS3 is4")
%>
3. Execute method
Performs a regular-expression search on the specified string. The design pattern of a regular expression search is set by RegExp object patterns.
The execute method returns a Matches collection that contains each of the matching Match objects found in string . If no match is found,execute will return an empty Matches collection.
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