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

Regular Expressions in JScript and JavaScript

Using the g attribute with String.match() produces multiple results in the way that you saw earlier with RegExp.exec(). If there are parentheses in the pattern, the results have an array of elements for each match, again resembling RegExp.exec().

The String.search() method takes a RegExp object as its argument. It returns the position of the first match in the string or -1 if there is no match.

The String.replace() method takes two arguments; the first is a RegExp object, and the second is the replacement string for matching text in the String object. The 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 and JScript 5.6.

Metacharacter

Description

 

 

\d

Matches a single numeric digit.

. (the period metacharacter)

Matches any character except the newline character or

 

another Unicode character for newline.

\w

Matches any ASCII word character — that is [A-Za-z0-9].

\W

Matches any character not matched by \w.

\s

Matches a whitespace character.

\S

Matches any character not matched by \s.

\D

Matches any character not matched by \d — that is,

 

\D is equivalent to [^0-9].

[...]

Character class. Matches any single character inside the

 

square brackets. Ranges are supported.

[^...]

Negated character class.

?

Quantifier. Indicates that zero or one occurrence of the pre-

 

ceding character or group matches.

*

Quantifier. Indicates that zero or more occurrences of the

 

preceding character or group matches.

+

Quantifier. Indicates that one or more occurrences of the

 

preceding character or group match.

{n,m}

Quantifier. Indicates that a minimum of n and a maximum

 

of m 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 or after the declaration of the variable to which the RegExp object is assigned.

For example, suppose that you had the following declaration:

var myRegExp = /\d{3}-\d{2}-\d{4}/;

You might want to document it like this:

var myRegExp = /\d{3}-\d{2}-\d{4}/;

//\d{3} matches three numeric digits

//- matches a hyphen

//\d{2} matches two numeric digits

//- matches a hyphen

//\d{4} matches four numeric digits

Or, if you prefer, document it like this:

var myRegExp = /\d{3}-\d{2}-\d{4}/; /* \d{3} matches three numeric digits

- matches a hyphen

\d{2} matches two numeric digits - matches a hyphen

\d{4} matches four numeric digits

*/

Either way, for complex patterns it is useful, particularly when doing code maintenance, to have a step- by-step description of the programmer’s intention in the documentation inside the code file. It can also be very useful when creating the code, because having to write documentation like this can sometimes throw up issues that you previously overlooked. Being forced to think formally like this does help you avoid some coding errors.

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

The test file, SSNValidation.html, is intended for use on Internet Explorer:

<html>

<head>

<title>Processing an SSN</title> <script language=”javascript” > myRegExp = /\d{3}-\d{2}-\d{4}/; var entry;

function Validate(){

entry = simpleForm.SSNBox.value; if (myRegExp.test(entry)) {

alert(“The value you entered, “ + entry + “\n matches the regular expression, “ + myRegExp + “. It is a valid SSN.” );

}// end the if statement else

{

alert(“The value you entered,” + entry + “,\nis not a valid SSN. Please try again.”);

}// end of else clause

}// end Validate() function

function ClearBox(){ simpleForm.SSNBox.value = “”;

// The above line clears the texbox when it receives focus } // end ClearBox() function

</script>

</head>

<body>

<form name=”simpleForm” > <table>

<tr>

<td width=”40%”>Enter a valid SSN here:</td>

<td><input name=”SSNBox” onfocus=”ClearBox()” type=”text” value=”Enter an SSN here”></input></td>

</tr>

<tr>

<td><input name=”Submit” type=”submit” value=”Check the SSN” onclick=”Validate()” ></input></td>

</tr>

</table>

</form>

</body>

</html>

How It Works

A pattern is specified that matches three numeric digits followed by a hyphen, followed by two numeric digits, followed by a hyphen, followed by four numeric digits:

myRegExp = /\d{3}-\d{2}-\d{4}/;

453

Chapter 19

Whether there is a match or not is determined by using the RegExp object’s test() method:

if (myRegExp.test(entry)) {

If matching is successful, a message is displayed that shows the user-entered text. If matching fails, the user is informed that the value entered is not an SSN.

Exercises

The following exercises are intended to allow you to test your understanding of some of the material discussed in this chapter:

1.Modify the FinalT.html example so that it will match a user entry that contains at least one numeric digit and contains only numeric digits.

2.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.

454