Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 78 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
78
Dung lượng
1,65 MB
Nội dung
Because the myRegExp variable was created with none of the associations specified, each of the global, ignoreCase, and multiline properties contains the Boolean value false. The source property contains the value of the pattern, \d+, which was contained in the assignment statement for the myRegExp variable. The lastIndex property contains the value of the character position following the previous match. When the test text was Hello 99, the value of the lastIndex property was 8, because the match (the character sequence 99) consisted of character positions 6 and 7. When the test text was 99 Hello, the value of the lastIndex property was 2, because the match, 99, consisted of character positions 0 and 1, because indexing starts at 0. The test() Method of the RegExp Object The test() method of the RegExp object tests whether or not a string matches a pattern. If there is at least one match, the test() method returns the Boolean value true. If there is no match, the test() method returns the Boolean value false. You saw in the preceding example situations that the value true was returned, because both test charac- ter sequences, Hello 99 and 99 Hello, contained at least one numeric digit and therefore matched the pattern \d+, which matches one or more numeric digits. If you enter a character sequence that contains no numeric digit, the value returned by the test() method when matching the pattern \d+ will be the Boolean value false. The exec() Method of the RegExp Object The exec() method of the RegExp object is powerful, flexible, and potentially confusing. First, let’s look at using the exec() method when the pattern is used with the g attribute. In other words, the value of the global property of the RegExp object will contain the Boolean value true. Try It Out With global Property true The test file, RegExpExecExample.html, contains the following markup and code: <html> <head> <title>RegExp exec() Method Example with global attribute set.</title> <script language=”javascript” type=”text/javascript”> var myRegExp = /\sthe/ig; var entry; function PatternProcess(entry){ var displayString = “”; while ((result = myRegExp.exec(entry)) != null ){ displayString += “Matched ‘“ + result; displayString += “‘ at position “ + result.index + “\n”; displayString += “The next match attempt begins at position “ + myRegExp.lastIndex; alert(displayString); displayString = “”; } // end while loop } // end function Process(entry) 441 Regular Expressions in JScript and JavaScript 22_574892 ch19.qxd 1/7/05 10:55 PM Page 441 function ShowPrompt(){ entry = prompt(“This script tests for matches for the regular expression pattern: “ + myRegExp + “.\nType in a string and click on the OK button.”, “Type your text here.”); PatternProcess(entry); } // end function ShowPrompt() </script> </head> <body> <form name=”myForm”> <br /> <button type=”Button” onclick=”ShowPrompt()”>Click here to enter text.</button> </form> </body> </html> 1. Open RegExpExecExample.html in Internet Explorer, and click the Click Here to Enter Text button. 2. In the text box, enter the following text: Hello there, the theatre is nice., which contains three matches for the pattern \sthe. Figure 19-11 shows the sample text entered in the prompt dialog box. Figure 19-11 3. Click the OK button, and inspect the alert box that is displayed, as shown in Figure 19-12. Notice that the matched text is the, which starts at position 6. Given the test string Hello there, the theatre is nice. , I hope you can see that the match the is the first three characters of there. Figure 19-12 4. Click the OK button, and inspect the next alert box that is displayed, as shown in Figure 19-13. Notice that the position of the matched text now begins at position 12, which indicates that the matching the is the word the in the test string. 442 Chapter 19 22_574892 ch19.qxd 1/7/05 10:55 PM Page 442 Figure 19-13 5. Click the OK button, and inspect the next alert box that is displayed, as shown in Figure 19-14. Notice that the position of the matched text now begins at position 16, which indicates that the matching the is the first three letters of theatre in the test string. Figure 19-14 How It Works As written, the code will display all matches in separate alert boxes. If there is no match, no message is displayed. The pattern to be matched is \sthe, which is a whitespace character followed by the character sequence the. The test string, Hello there, the theatre is nice., contains three character sequences that match the pattern \sthe. The first match is the space character after the word Hello followed by the character sequence the in the word there. Numbering of character positions starts at 0. Position 5 is the position before the first matching character, which is a space character. Any further matching would continue after the e of the at position 9. The second match is the space character after the comma following the word there. The following char- acter sequence the is the word the. The third match is the space character after the word the, followed by the first three characters of the word theatre. When the exec() method of the RegExp object is used in a nonglobal matching process, it matches only once. It returns parts of the matching character sequence that correspond to parts of the pattern that are within parentheses in an array. It is, perhaps, easier to demonstrate what is returned, rather than attempt to describe it in isolation. 443 Regular Expressions in JScript and JavaScript 22_574892 ch19.qxd 1/7/05 10:55 PM Page 443 Try It Out The exec() Method in Nonglobal Matching The sample file, RegExpExecNonGlobal.html, is shown here: <html> <head> <title>RegExp exec() Method Example with no global attribute.</title> <script language=”javascript” type=”text/javascript”> var myRegExp = /((A|B)(\d{3}))/i; var entry; function PatternProcess(entry){ var displayString = “”; result = myRegExp.exec(entry); for (n=0; n<5; n++){ displayString += “Matched ‘“ + result[n]; displayString += “‘ in result[“ + n + “].\n”; } // end for loop alert(displayString);displayString = “”; } // end function Process(entry) function ShowPrompt(){ entry = prompt(“This script tests for matches for the regular expression pattern: “ + myRegExp + “.\nType in a string and click on the OK button.”, “Type your text here.”); PatternProcess(entry); } // end function ShowPrompt() </script> </head> <body> <form name=”myForm”> <br /> <button type=”Button” onclick=”ShowPrompt()”>Click here to enter text.</button> </form> </body> </html> 1. Open RegExpExecNonGlobal.html in Internet Explorer, and click the Click Here to Enter Text button. 2. Enter a part number, A234, in the text box in the prompt dialog box, and click the OK button. 3. Inspect the alert box that is displayed, as shown in Figure 19-15. Figure 19-15 444 Chapter 19 22_574892 ch19.qxd 1/7/05 10:55 PM Page 444 How It Works In result[0], the whole of the matching character sequence is returned— in this case, A234. In result[1], the matching character sequence contained in the outermost parentheses is returned. In this example, that is also A234. In result[2], the matching character sequence contained in the next set of nested parentheses is returned. In this case, that is A. In result[3], the matching character sequence contained in the next set of nested parentheses is returned. In this case, that is 234. The array element result[4] was added to demonstrate that the value undefined is returned when there is no further pair of matching parentheses. The next Try It Out section puts the two situations together to look in more detail at using parentheses and global matching. Try It Out Parentheses and Global Matching with exec() The test file, RegExpExecExample2.html, is shown here: <html> <head> <title>RegExp exec() Method Example with global attribute set.</title> <script language=”javascript” type=”text/javascript”> var myRegExp = /((A|B|C)(\d{3}))/ig; var entry; function PatternProcess(entry){ var displayString = “”; while ((result = myRegExp.exec(entry)) != null ){ displayString += “Matched ‘“ + result; displayString += “‘ at position “ + result.index + “\n”; displayString += “The next match attempt begins at position “ + myRegExp.lastIndex + “\n”; displayString += “The whole matching string is “ + result[0] + “\n”; displayString += “The content of the outer parentheses is “ + result[1] + “\n”; displayString += “The content of the first nested parentheses is “ + result[2] + “\n”; displayString += “The content of the second nested parentheses is “ + result[3] + “\n”; alert(displayString); displayString = “”; } // end while loop } // end function Process(entry) function ShowPrompt(){ entry = prompt(“This script tests for matches for the regular expression pattern: “ + myRegExp + “.\nType in a string and click on the OK button.”, “Type your text here.”); PatternProcess(entry); } // end function ShowPrompt() 445 Regular Expressions in JScript and JavaScript 22_574892 ch19.qxd 1/7/05 10:55 PM Page 445 </script> </head> <body> <form name=”myForm”> <br /> <button type=”Button” onclick=”ShowPrompt()”>Click here to enter text.</button> </form> </body> </html> The matching is global, as indicated by the g attribute in the declaration of the myRegExp variable. It also includes parentheses. 1. Open RegExpExecExample2.html in Internet Explorer, and click the Click Here to Enter Text button. 2. Enter the test text A123, B456, C789 in the text box, and click the OK button. 3. Inspect the results displayed in the first alert box, as shown in Figure 19-16. (The results will be discussed in the How It Works section that follows.) Figure 19-16 4. Click the OK button to dismiss the first alert box, and inspect the results displayed in the second alert box, as shown in Figure 19-17. Figure 19-17 5. Click the OK button to dismiss the second alert box, and inspect the results displayed in the third alert box, as shown in Figure 19-18. 446 Chapter 19 22_574892 ch19.qxd 1/7/05 10:55 PM Page 446 Figure 19-18 How It Works The myRegExp variable is declared in the following statement: var myRegExp = /((A|B|C)(\d{3}))/ig; This pattern, ((A|B|C)(\d{3})), will match character sequences that begin with A, B, or C followed by three numeric digits. Not all the parentheses are necessary, but they help illustrate what the exec() method returns in the array of results when parentheses are present. The following test string contains three matches for the pattern ((A|B|C)(\d{3})): A123, B456, and C789: A123, B456, C789 First, look in detail at what is displayed in the first alert box, which you can inspect in Figure 19-16. The information displayed in that alert box is built up in the following code: displayString += “Matched ‘“ + result; displayString += “‘ at position “ + result.index + “\n”; displayString += “The next match attempt begins at position “ + myRegExp.lastIndex + “\n”; displayString += “The whole matching string is “ + result[0] + “\n”; displayString += “The content of the outer parentheses is “ + result[1] + “\n”; displayString += “The content of the first nested parentheses is “ + result[2] + “\n”; displayString += “The content of the second nested parentheses is “ + result[3] + “\n”; It is displayed using the following code: alert(displayString); The first line of the alert box displays each part of the result array separated by commas. As you can see in Figure 19-16, it reads Matched ‘A123, A123, A, 123’ at position 0. The commas separate the content of result[0], result[1], result[2], and result[3]. Matching begins at position 0. 447 Regular Expressions in JScript and JavaScript 22_574892 ch19.qxd 1/7/05 10:55 PM Page 447 The next four lines in the alert box in Figure 19-16 spell out how each part of the result array is arrived at. The next match is B456, and the results for that are displayed in Figure 19-17. Notice that the position is now 6. The elements of the result array are displayed as just described. The third match is C789. The results are displayed in Figure 19-18. Notice that the position is now 12. The elements of the result array are displayed as described earlier in this How It Works section. The String Object In JavaScript/JScript, a string is a sequence of Unicode characters enclosed in paired quotation marks or apostrophes. The JavaScript/JScript String object represents a string. For many purposes, the value of a JavaScript string is the same as a character sequence. A String object represents a programmatic inter- face to such a sequence of characters. The following lines of code each contain an example of a JavaScript/JScript string: “Test” ‘This is a multicharacter string enclosed in paired apostrophes.’ “99.31” “This string has two \n lines.” A string must be written on a single line. However, a multiline string can be represented using the \n escape sequence notation to represent a newline. Three methods of the String object are relevant to the use of regular expressions: ❑ match() ❑ replace() ❑ search() The match() method takes a RegExp object as its argument and tests whether the string is a match for the pattern associated with that object. Try It Out The String.match() Method The test file for the String.match() example is StringObjectMatch.html, whose code is shown here: <html> <head> <title>The match() Method of the String Object.</title> <script language=”javascript” type=”text/javascript”> var myRegExp = /\d+\.\d+/; var entryString; var displayString = “”; function StringProcess(){ 448 Chapter 19 22_574892 ch19.qxd 1/7/05 10:55 PM Page 448 if (entryString.match(myRegExp) != null ){ var result = entryString.match(myRegExp); displayString += “Matched ‘“ + result + “.\n”; displayString += “result[0] is “ + result[0] + “.\n”; displayString += “result[1] is “ + result[1] + “.\n”; alert(displayString); displayString = “”; } // end if statement else alert(“The string you entered did not match the pattern “ + myRegExp); } // end function StringProcess() function ShowPrompt(){ entryString = prompt(“Type a string which is or contains a decimal number.\nType and click on the OK button.”, “Type a pattern here.”); StringProcess(); } </script> </head> <body> <form name=”myForm”> <br /> <button type=”Button” onclick=”ShowPrompt()”>Click here to enter a decimal value.</button> </form> </body> </html> The example will accept a string that contains or consists of a decimal number. To match the pattern \d+\.\d+, the decimal number must contain at least one numeric digit before the decimal point and at least one numeric digit after the decimal point. 1. Open StringObjectMatch.html in Internet Explorer, and click the Click Here to Enter a Decimal Value button. 2. In the text box in the prompt dialog box, enter the string My score is 91.23, and click the OK button. 3. Inspect the information displayed in the alert box, as shown in Figure 19-19. Figure 19-19 449 Regular Expressions in JScript and JavaScript 22_574892 ch19.qxd 1/7/05 10:55 PM Page 449 [...]... replacement string can be specified literally or can be a function call Metacharacters in JavaScript and JScript JavaScript and JScript regular expressions are based on Perl regular expressions Just as Perl regular expression support has evolved over time, so the support for regular expressions has evolved in JScript and JavaScript The following table summarizes the metacharacters supported in JavaScript 1.5... occurrences of the preceding character or group match 451 Chapter 19 Documenting JavaScript Regular Expressions A number of languages support the use of extended whitespace for regular expressions Unfortunately, that feature is not supported in JavaScript or JScript I suggest that for anything but fairly trivial regular expression patterns, you document the pattern in comment lines immediately before... input box: A9 B10 C110 D1123456 E12345 678 90 A3 3 Click the OK button, and inspect the results displayed in the message box, as shown in Figure 20-11 Figure 20-11 How It Works When the Web page loads, the MatchLength function is called: The regular expression pattern assigned to the Pattern property: myRegExp.Pattern = “[A-Z]\d+” 472 Regular Expressions and VBScript matches a... numeric digits Modify SSNValidation.html so that it will match a 16-digit credit card number that is entered in groups of four numeric digits separated by a whitespace character 20 Regular Expressions and VBScript Regular expressions were introduced to VBScript in version 5.0 They can be used to parse character sequences (strings) and can be used to provide flexible replace functionality VBScript can... and the Matches collection ❑ The metacharacters supported in VBScript and how to use them ❑ How to use VBScript regular expressions to solve some text-handling problems The RegExp Object and How to Use It The RegExp object, the Match object, and the Matches collection all relate to how regular expressions are used in VBScript This section focuses on the RegExp object The RegExp object has three properties... SSN Validation Example You can use regular expressions with JavaScript and JScript to validate information entered into a form on a Web page This example will validate the structure of a U.S Social Security number (SSN) An SSN has three numeric digits followed by a hyphen, followed by two numeric digits, followed by a hyphen, followed by four numeric digits 452 Regular Expressions in JScript and JavaScript... Click the OK button, and inspect the results displayed in the message box, as shown in Figure 20-10 Notice that each match in the Matches collection is now listed in the message box Figure 20-10 470 Regular Expressions and VBScript How It Works The code works similarly to the code in ExecuteDemo.html The crucial difference is that the value of the myRegExp variable’s Global property is set to a value... ExecuteDemo are dimensioned: Function ExecuteDemo Dim myRegExp, TestName, Match, Matches, displayString 468 Regular Expressions and VBScript The value of the displayString variable is set to the empty string, and a reference to a new RegExp object is set: displayString = “” Set myRegExp = new RegExp The regular expression pattern [A-Z]\d, which matches an alphabetic character followed by a numeric digit,... [^A-Za-z0-9_] Quantifiers VBScript supports a full range of quantifiers — that is, the ?, *, + metacharacters, together with the {n,m} notation The usage of these quantifiers in VBScript is standard 474 Regular Expressions and VBScript Positional Metacharacters The ^ metacharacter is supported and matches the position before the first character of a character sequence The $ metacharacter is also supported... VBCrLf _ & “The changed string is “ & ChangedString Else MsgBox “There is no match ‘“ & InputString & “‘ does not match “ &VBCrLf _ & “the pattern ‘“ & myRegExp.Pattern & “‘.” End If End Function 462 Regular Expressions and VBScript Function DoReplaceInsensitive myRegExp.IgnoreCase = True InputString = InputBox(“Enter a string It will be tested to see if it contains” &VBCrLf & “any ‘A’ characters Any ‘A’ . JScript JavaScript and JScript regular expressions are based on Perl regular expressions. Just as Perl regular expression support has evolved over time, so the support for regular expressions has evolved. result[2], and result[3]. Matching begins at position 0. 4 47 Regular Expressions in JScript and JavaScript 22_ 574 892 ch19.qxd 1 /7/ 05 10:55 PM Page 4 47 The next four lines in the alert box in Figure 19-16. preceding character or group match. 451 Regular Expressions in JScript and JavaScript 22_ 574 892 ch19.qxd 1 /7/ 05 10:55 PM Page 451 Documenting JavaScript Regular Expressions A number of languages support