Tutorials | Getting Started | get Started | First, let's take a look at what the "regular expression" object is in VBScript, let's start with a procedure:
Function checkexp (PATRN, STRNG)
Dim regEx, Match ' establishes the 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.
matches = regex.test (strng) ' performs the search.
Checkexp = Matches
End Function
In this program, we see that we can use "New RegExp" to get a regular expression object, and then make a regular matching template assignment to that object, which tells the regular expression object what kind of template you want to match. Then use method test to detect whether the data to be processed and we give the template is matched, if not match, it indicates that the data to be processed is not legitimate data, thus achieving the validation of data legality, we can see that, using a reasonable design matching template, We can easily verify a batch of similar format data information.
Of course, the regular expression object in VBScript5.0 has many other methods and properties, such as method replace (), so that we can quickly realize the current online very fashionable UBB style forum and BBS, which is not within the scope of our discussion, to be discussed later, Let's take a look at it. Methods and properties commonly used in data validation for regular expression objects:
Common method: Execute method
Description: Performs a regular-expression search on the specified string.
Syntax: Object. The syntax of the Execute (String) Execute method includes the following sections:
Object: Required. Always the name of a RegExp object.
String: Required. The text string on which to execute the regular expression.
Description: The design pattern of regular expression search is set by RegExp object patterns. The Execute method returns a matches collection that contains each matching match object found in string. If no match is found, Execute returns an empty matches collection.
Test method
Description: Performs a regular expression search on the specified string and returns a Boolean indicating whether the matching pattern is found.
Syntax: Object. Test (String)
The syntax of the Test method includes the following sections:
Object: Required. Always the name of a RegExp object.
String: Required. The text string to perform a regular expression search for.
Description: The actual pattern of the regular expression search is set by the RegExp object's Patterns property. 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.
Common Properties: Global properties
Description: Sets or returns a Boolean value that indicates whether the pattern matches all or only the first one throughout the search string.
Syntax: Object. Global [= True | False]
The object argument is always REGEXP objects. 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 True.
IgnoreCase Property
Description: Sets or returns a Boolean value that indicates whether the pattern search is case-sensitive.
Syntax: Object. IgnoreCase [= True | False]
The object argument is always a RegExp object. If the search is case-sensitive, the IgnoreCase property is False; otherwise, true. The default value is True.
Pattern Properties
Description: Sets or returns the regular expression pattern that is searched. This is one of the most important attributes, and we mainly set this property to implement data validation.
Syntax: Object. pattern [= "searchstring"]
The syntax of the pattern attribute consists of the following sections:
Object: Required. Always a REGEXP object variable.
SearchString: Optional. The regular string expression being searched. It may contain various regular expression characters in the Set section table.
Settings: Special characters and sequences are used when writing patterns for regular expressions. The following table describes the characters and sequences that can be used, and gives an example.
Character description: \: Marks the next character as a special character or literal. For example, "n" matches the character "n". "\ n" matches the line feed character. The sequence "\" and "\" match, "\" ("Match").
^: matches the start position of the input.
$: Matches the end of the input.
*: Matches the previous character 0 times or several times. For example, "zo*" can Match "Z", "Zoo".
+: Matches the previous character one or more times. For example, "zo+" can Match "zoo", but does not match "Z".
? : matches the previous character 0 times or once. For example, "A?ve?" Can match "never" in the "VE".
.: matches any character other than the newline character.
(pattern) matches patterns and remembers matches. The matching substring can be used with the Item [0] from the matches collection as the result ... [n] obtained. If you want to match the bracket characters (and), you can use "\ (" or "\)".
X|y: Matches x or Y. For example, "Z|food" can match "Z" or "food". "(z|f) Ood" matches "zoo" or "food".
{N}:n is a non-negative integer. Matches exactly n times. For example, "o{2}" cannot match "O" in Bob, but can match the first two o in "Foooood".
{N,}: N is a non-negative integer. Match at least n times. For example, "o{2,}" does not match "O" in "Bob", but matches all o in "Foooood". "O{1,}" is equivalent to "o+". "O{0,}" is equivalent to "o*".
{n,m}: M and n are non-negative integers. Match at least n times, at most m times. For example, "o{1,3}" matches the first three o in "Fooooood". "o{0,1}" is equivalent to "O?".
[XYZ]: a character set. Matches one of the characters in parentheses. For example, "[ABC]" matches "a" in "plain".
[^XYZ]: a negative character set. Matches any character that is not in this bracket. For example, "[^ABC]" can match "P" in "plain".
[A-Z]: represents a range of characters. Matches any character within the specified interval. For example, "[A-z]" matches any of the lowercase alphabetic characters between "a" and "Z".
[^m-z]: a negative character range. Matches characters that are not in the specified interval. For example, "[M-z]" matches any character that is not between "M" and "Z".
\b: Matches the boundary of a word, which is the position between a word and a space. For example, "er\b" matches "er" in "never", but does not match "er" in "verb".
\b: Matches a non word boundary. "ea*r\b" matches the "ear" in "Never early".
\d: Matches a numeric character. equivalent to [0-9].
\d: Matches non-numeric characters. equivalent to [^0-9].
\f: Matches page breaks.
\ n: matches the line break character.
\ r: Matches the carriage return character.
\s: Matches any white character, including spaces, tabs, page breaks, and so on. Equivalent to "[\f\n\r\t\v]".
\s: Matches any non-white-space character. Equivalent to "[^ \f\n\r\t\v]".
\ t: Matches tabs.
\v: Matches the Vertical tab.
\w: Matches any word characters, including underscores. Equivalent to "[a-za-z0-9_]".
\w: matches any non word character. Equivalent to "[^a-za-z0-9_]".
\num: Matches num, where num is a positive integer. References are returned to the remembered match. For example, "(.) \1 "matches two consecutive identical characters.
\ n: Match n, where n is a octal value. The octal change value must be 1, 2 or 3 digits long.
For example, both "\11" and "\011" match one tab. "\0011" is equivalent to "\001" and "1". The value of the octal converter cannot exceed 256. Otherwise, only the first two characters are considered part of an expression. Allows ASCII code to be used in regular expressions.
\xn: Matches n, where n is a hexadecimal code-changing value. The hexadecimal transposition value must be exactly two digits long. For example, "\x41" matches "A". "\x041" is equivalent to "\x04" and "1". Allows ASCII code to be used in regular expressions.
Okay, commonly used methods and attributes are these, the above syntax is very detailed, we do not need to wordy, and then we look at the specific examples of how to use these methods and attributes to verify the legality of the data, we still give an example, for example, We want to verify the email that the user entered, so what kind of data is a legitimate email? I can enter this way: uestc95@263.net, of course I will enter this way: xxx@yyy.com.cn, but such input is illegal: xxx@ @com. cn or @xxx.com.cn, etc., so we come up with a legitimate email address that should meet at least several of the following conditions:
1. Must contain one and only one symbol "@"
2. Must contain at least one maximum of three symbols "."
3. The first character must not be "@" or "."
4. "@." is not allowed to appear. Or. @
5. The end must not be the character "@" or "."
So based on the above principles and the syntax in the table above, it's easy to get the template you need: "(\w) +[@]{1} (\w) +[.] {1,3} (\w) + "
Next, we'll look at this template, first "\w" indicates that the beginning character of a message can only be a word character that contains an underscore, so that the third condition is met, and the "[@]{1}" means that it should match the character "@" in an e-mail message, and that the condition one is satisfied; {1,3} "means to match at least 1 matches of up to 3 characters in an e-mail message.", which satisfies the second condition, the final "(\w) +" character of the template can only be a word character containing an underscore, satisfies the condition five, the "(\w) +" in the middle of the template satisfies the condition four.
Then we'll just call that function Checkexp ("(\w) +[@]{1} (\w) +[." {1} (\w) + ", to check the string) is good, if the return of true to indicate that the data is legitimate, otherwise it is not correct, how, simple. We can also write a template that verifies the ID number: "([0-9]) {15}"; Template for validating URLs: "^http://{1} ((\w) +[.]) {1,3} "and so on; we can see that these templates provide us with a good reusable module, using their own or other provided by the various templates, we can easily and quickly verify the validity of the data, I believe you will write a very common template.
In this way, as long as we customize different templates, we can achieve the validity of different data validation. Therefore, the most important attribute of the regular expression object is: "pattern" attribute, as long as the real grasp of this attribute, you can freely use regular expression object to our data validation services.
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") & "
" 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 matching match object found in string. If no match is found, Execute returns an empty matches collection.
After JavaScript version 1.2, JavaScript also supports regular expressions.
1, replace
Replace replaces the corresponding content with a regular expression lookup in a string. Replace does not change the original string, just regenerates a new string. If you need to perform a global lookup or ignore case, then add G and I at the end of the regular expression.
Cases:
The result is: "Oranges are round, and oranges are juicy."
Cases:
The result is: "Twas the night Before Christmas ..."
Cases:
The result: "Smith, John."
2. Search
Search uses regular expressions to find the appropriate string, just to determine if there is a string that does not match. If the search succeeds, search returns the location of the matching string, or returns 1.
Search (RegExp)
3, Match
The match method performs a global lookup, and the lookup results are stored in an array.
Example one:
Display results: Chapter 3.4.5.1,chapter 3.4.5.1,.1
Case TWO:
Show Results D, D.
1, judge the correctness of the number
<%@ Language=vbscript%>
<script language= "javascript" runat= "Server"
function IsNumeric (strnumber) {
Return (Strnumber.search/^ (-|\+) \d+ (\.\d+) $/)!=-1);
}
function Isunsignednumeric (strnumber) {
Return (Strnumber.search/^\d+ (\.\d+) $/)!=-1);
}
function Isinteger (Strinteger) {
Return (Strinteger.search/^ (-|\+) \d+$/)!=-1);
}
function Isunsignedinteger (Strinteger) {
Return (Strinteger.search (/^\d+$/)!=-1);
}
</script>
<HTML>
<BODY>
<b> judge the correctness of numbers </b>
<%
Dim strtemp
strtemp = CStr (Request.Form ("inputstring"))
If strtemp = "" Then strtemp = "0"
%>
<table border= "1" cellpadding= "4" cellspacing= "2" >
<TR>
<TD align= "Right" <B> original string </B> </TD>
<TD> <%= strtemp%> </TD>
</TR>
<TR>
<TD align= "right" > <B> number </B> </TD>
<TD> <%=isnumeric (strtemp)%> </TD>
</TR>
<TR>
<TD align= "right" > <B> non-negative number </B> </TD>
<TD> <%=isunsignednumeric (strtemp)%> </TD>
</TR>
<TR>
<TD align= "right" > <B> integer </B> </TD>
<TD> <%=isinteger (strtemp)%> </TD>
</TR>
<TR>
<TD align= "Right" <B> nonnegative integer () </B> </TD>
<TD> <%=isunsignedinteger (strtemp)%> </TD>
</TR>
</TABLE>
<form action= "<%=request.servervariables (" Script_name ")%>" method= "POST"
Please enter a number: <BR>
<input type= "text" name= "inputstring" size= "" "</INPUT> <BR>
<input type= "Submit" value= "Submission" > </INPUT> <BR>
</FORM>
</BODY>
</HTML>
2, to determine the correctness of the email address
<%
Function Isemail (STRNG)
Isemail = False
Dim RegEx, Match
Set regEx = New RegExp
Regex.pattern = "^\w+ (-\w+) | ( \.\w+)) *\@[a-za-z0-9]+ (\.| -) [a-za-z0-9]+) *\. [a-za-z0-9]+$]
Regex.ignorecase = True
Set Match = Regex.execute (strng)
If Match.count then isemail= true
End Function
%>