| home / programming / javascript / professional / chap3 / 5 |
[next] |
|
Professional JavaScriptRegular ExpressionsLast, but definitely not least, we come to the topic of Regular Expressions (RE), a not-so-simple pattern of characters that can be used to match a sequence of characters in a string. Combined with the right method, regular expressions can perform some pretty heavyweight text search and replace duties. This is not limited to checking that someone has typed in the right kind of phone number in a form element or validating some other kind of data. We could, for instance, search through a bank's records and add new digits to everyone's bank account number or discover the number of times Homer says "Doh!" in the screenplay for an episode of The Simpsons. JavaScript has had built-in support for Regular Expressions since version 1.2 in the shape of the RegExp object and a few certain methods attached to the String object, all of which we'll come to later, but before we have a look at these, we need to nail down how exactly you construct such an expression. Rolling Your Own REWhen it comes to producing your own regular expressions, you have two options in JavaScript – you can write them either as literals or as objects. For example
Both these lines do the same thing – assign myRE with a reference to a newly created RegExp object whose expression will match an instance of the sequence "R2D2" in a string. The RegExp object contains the data for the RE you specify when that object is created. Easy. However, you can't really make use of the full power of regular expressions until you learn their alphabet and syntax. The first point easily learned is that with the exception of two switches, every RE literal is contained within a pair of forward slashes.
The two switches mentioned earlier are g and i, and affect directly how the search to match your regular expression is conducted. g, the global switch, tells the search to find every instance of your character sequence in the target string, rather than just to find the first and then stop looking. i, meanwhile, tells the search mechanism that the search is case insensitive. For instance:
With that out of the way, we'd better look and see what we can put inside the slashes. The RE AlphabetThe alphabet for regular expressions incorporates all the alphanumeric characters, upper and lower case, and quite a few other special characters in the form of escape sequences, as shown below. Note that an escape sequence may match one or more ordinary characters or alternatively a special condition that isn't an ordinary character, like the start of a string, as we'll see in the pages to come.
You should be familiar with what the first ten of these items will match up to – we've encountered them already at the very beginning of Chapter 1, so to demonstrate the rest, let's take an example text Jimmy the Scot scooted his scooter through the park. The Parky watched Jimmy do this and we'll go through some easy examples:
From these examples, you can see that each character or escape character between the forward slashes of a regular expression stands for a single character only. Some escape characters will match more than one kind of normal character such as the whitespace escape sequence, but only match one character in total.
|
| home / programming / javascript / professional / chap3 / 5 |
[next] |
Created: February 12, 2001
Revised: February 21, 2001