Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Regular Expressions 2005.pdf
Скачиваний:
95
Добавлен:
17.08.2013
Размер:
25.42 Mб
Скачать

Chapter 19

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 interface 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

Regular Expressions in JScript and JavaScript

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

Chapter 19

How It Works

The variable myRegExp is assigned the regular expression pattern \d+\.\d+:

var myRegExp = /\d+\.\d+/;

This will match decimal numbers, provided that there is at least one numeric digit both before and after the decimal point.

Clicking the Click Here to Enter a Decimal Value button calls the ShowPrompt() function, which requests user input. The user input is assigned to the variable entryString:

entryString = prompt(“Type a string which is or contains a decimal number.\nType

and click on the OK button.”, “Type a pattern here.”);

And then the ProcessString() function is called.

First, an if statement tests whether there is a match or not:

if (entryString.match(myRegExp) != null )

If there is no match, entryString.match(myRegExp) will return null, and the processing of the function will proceed through the else clause (shown a little later). If there is a match, entryString

.match(myRegExp) is not null, so processing of the if statement proceeds.

The result variable is an array (because you know there is a match) where element result[0] contains the matching character sequence. The elements result[1] and higher contain any components of the match as signified by parentheses in the regular expression pattern. This resembles the behavior of the exec() method of the RegExp object:

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 = “”;

The variable displayString is declared globally as an empty string:

var displayString = “”;

Then, inside the if statement, its content is built up using the value of result, result[0], and result[1]. As you can see in Figure 19-19, the value of result[1] is undefined, because the pattern \d+\.\d+ contains no parentheses. Finally, after the alert box is dismissed, the value of displayString is again assigned the empty string as its value. This is necessary because you are not submitting the data anywhere.

If entryString.match(myRegExp) returns null, the else clause will be processed, which displays a message stating that there was no match.

450