DekGenius.com
Team LiB   Previous Section   Next Section

3.10 Regular Expressions

Regular expressions provide a rich and powerful syntax for describing textual patterns; they are used for pattern matching and for implementing search and replace operations. JavaScript has adopted the Perl programming language syntax for expressing regular expressions. Regular expression support was first added to the language in JavaScript 1.2 and was standardized and extended by ECMAScript v3.

Regular expressions are represented in JavaScript by the RegExp object and may be created using the RegExp( ) constructor. Like the Date object, the RegExp object is not one of the fundamental data types of JavaScript; it is simply a specialized kind of object provided by all conforming JavaScript implementations.

Unlike the Date object, however, RegExp objects have a literal syntax and can be encoded directly into JavaScript 1.2 programs. Text between a pair of slashes constitutes a regular expression literal. The second slash in the pair can also be followed by one or more letters, which modify the meaning of the pattern. For example:

/^HTML/
/[1-9][0-9]*/
/\bjavascript\b/i 

The regular expression grammar is complex and is documented in detail in Chapter 10. At this point, you need only know what a regular expression literal looks like in JavaScript code.

    Team LiB   Previous Section   Next Section