Availability: |
| ||||||||
Property/method value type: | Array object | ||||||||
JavaScript syntax: | - | myRegExp.exec() | |||||||
- | myRegExp.exec(aString) | ||||||||
Argument list: | aString | A string object to run a regular expression match against. |
This is functionally similar to the String.match() method. It returns an enhanced array object in the same way. You would use this if you need to know the location of the matched string and whether it occurs more than once.
The g attribute is ignored for RegExp.exec() methods unlike the String.match() method.
When the exec() method is received by a global RegExp object, the lastIndex property of that RegExp object is set to point at a character location immediately following the previous match. This means you can use sub-stringing techniques to walk through the string calling exec() in an iterator until a null value is returned. This lets you build an iterator based on the pattern and the number of times it recurs in the searched string. The RegExp.exec() method does most of the work for you since it continues where it left off during the previous search. This also means you can reset the search point or commence searching wherever you like in the target string.
A short-cut mechanism to calling the exec() method is to call the RegExp itself as a function. Thus we can create a regular expression object and a target string object:
myRegExp = new RegExp("/\\d+/");
myString = "aaa 111 bbb 222 ccc";
We can execute the regular expression either like this:
myRegExp.exec(myString);
Or like this:
myRegExp(myString);
The result of calling this method is the null value if no match occurs. Otherwise, an array as per the String.match() method is returned.
The array object has an additional property named index, which contains the character location where the match occurred. It also has an additional property called input, which contains the original string that was searched for a match.
Support for this method is bugged in IE 4.
If you do not pass a string to the RegExp.exec() method, it will match against the current value of the RegExp.input property that belongs as a static class property of the built-in RegExp object.
The input property gets set automatically by client-side event handlers for FormElement objects in a web page form.
Prev | Home | Next |
RegExp.constructor | Up | RegExp.global |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |