Professional JavaScript | 14 | WebReference

Professional JavaScript | 14


Professional JavaScript

RE Methods and Properties

Now you know how to write your very own patterns, what do you do with them? Well, we've already discovered that there is a RegExp object that is automatically created when you assign a string pattern to a variable. Now it's time to meet the methods we use to do our dirty work with. There are two attached to the RegExp object and four to the String object that work with REs, as follows.

The String object's search() method is the simplest of all the operations we cover here.

var str="96521234";
var reg= new RegExp("965");
var reg2= /123/;
var;              // index = 0
var (reg2);            // index2 = 4

As you can guess from our example, search simply looks through the String specified and returns to index the position of the first matching character sequence in the String. Note that String ignores the global switch 'g' in a RE literal and that this index value begins at 0 as line 4 demonstrates. If a match is not found, search returns -1.


The split() method has been in the language since JavaScript 1.1, but with version 1.2 came support for it to take a regular expression argument.

var large_number = "212,0,456,0,67889";
var reg3 = /,\d,/;
var numberList = large_number.split(re);
                                        // numberList = ["212","456","67889"]

The purpose of split() is to take a string and return an array of string elements. Each element exists in the original string separated by characters matching the pattern sent to split() as its argument.


As you would imagine, the replace() method returns a brand new string that contains a copy of the original string with any matching part of it replaced accordingly. For example

reg4 = /happy/gi;
str4 = "I'm happy. You're Happy";
newstr4=str4.replace(reg4, "sad");   // newstr4 = "I'm sad. You're sad"

Notice the use of the global and case-insensitive switches. Without them newstr4 would be assigned "I'm sad. You're Happy".

replace() is actually a lot more useful than it would appear from this simple example. Recall from part two of our syntax discussion the use of /1, /2, etc to refer to the match for a parenthesized sub-expression. The RegExp object has similar properties called $1, $2. etc up to $9, equivalent to /1 and so on which can be fed back into replace(). For example

reg5 = /(I am)(\s\w*.\s)(You are)(\s\w*)/;
str5 = "I am working. You are asleep";
newstr5 = str5.replace(reg5, "$3$2$1$4");
                                    //newstr5 = "You are working. I am asleep"

As you can guess, $1 and $3 correspond to "I am" and "You are" respectively and are swapped accordingly by replace().


Our last string method, match(), is very similar to replace() except that instead of returning a new string, it returns an array of matches to the global regular expression as a result.

In the case that the regular expression does not contain the global switch, the first element of the array will always return the match for the complete expression while subsequent elements will hold $1, $2, etc.

reg6 = /(I am)(\s\w*.\s)(You are)(\s\w*)/;
str6 = "I am working. You are asleep";
var newarr6 = str6.match(reg6);

So in this example, newarr[0] holds "I am working. You are asleep", newarr[1] holds "I am", newarr[2] holds " working. " and so on.


The first of the methods attached to the RegExp object is very similar to the search() method we looked at earlier. It simply returns a boolean value, true or false, depending on whether or not a pattern can be matched to a sequence of characters in the given string. For example:

var string="96521234";
var reg= /965/;
var isin = reg.test(string);            // isin = true

If the pattern has the global flag set, it will set the lastIndex property of the RegExp object (see below) and continue the search from that point in the string when called again. If it does not have the flag set, lastIndex will be reset to 0.


The last of our RE-utilizing methods is exec(), which acts in a similar fashion to match() when the global switch is not used. It also has useful side-effects. Let's start with an example.

var string="965212234";
var reg= /(\d{2}2)/g;                   //Look for two digits followed by a 2
var results = reg.exec(string);         // = ["652", "652"]   Call 1
var results = reg.exec(string);         // = ["122", "122"]   Call 2

exec does a little more than you may first have guessed. In fact it populates all the static properties of the RegExp object, the reg object and updates details of the array too. exec() also behaves the same as test() with respect to the global flag being set. Should it not find a match, exec() returns null for the array.

Given the code above, the following tables demonstrate what each call to reg.exec(string) populates various properties with.

Created: February 12, 2001
Revised: February 26, 2001