Saturday, March 17, 2007

Working With Regular Expressions

Regular expressions are used with the RegExp methods test and exec and with the String methods match, replace, search, and split.These methods are explained in detail in the Core JavaScript Reference.


Methods that use regular expressions

Method:Description

exec:A RegExp method that executes a search for a match in a string. It returns an array of information.

test:A RegExp method that tests for a match in a string. It returns true or false.

match:A String method that executes a search for a match in a string. It returns an array of information or null on a mismatch.

search:A String method that tests for a match in a string. It returns the index of the match, or -1 if the search fails.

replace:A String method that executes a search for a match in a string, and replaces the matched substring with a replacement substring.

split:A String method that uses a regular expression or a fixed string to break a string into an array of substrings.

When you want to know whether a pattern is found in a string, use the test or search method; for more information (but slower execution) use the exec or match methods. If you use exec or match and if the match succeeds, these methods return an array and update properties of the associated regular expression object and also of the predefined regular expression object, RegExp. If the match fails, the exec method returns null (which converts to false).

In the following example, the script uses the exec method to find a match in a string.

<script LANGUAGE="JavaScript1.2">
myRe=/d(b+)d/g;
myArray = myRe.exec("cdbbdbsbz");
</script>

If you do not need to access the properties of the regular expression, an alternative way of creating myArray is with this script:

<script LANGUAGE="JavaScript1.2">
myArray = /d(b+)d/g.exec("cdbbdbsbz");
</script>

If you want to construct the regular expression from a string, yet another alternative is this script:

<script LANGUAGE="JavaScript1.2">
myRe= new RegExp ("d(b+)d", "g");
myArray = myRe.exec("cdbbdbsbz");
</script>

With these scripts, the match succeeds and returns the array and updates the properties shown in the following table.

Results of regular expression execution.









































Object

Property or index

Description

In this example

myArray


The matched string and all remembered substrings.

["dbbd", "bb"]

index

The 0-based index of the match in the input string.

1

input

The original string.

"cdbbdbsbz"

[0]

The last matched characters.

"dbbd"

myRe
lastIndex
The index at which to start the next match.
(This property is set only if the regular expression uses the g option, described in Executing a Global Search, Ignoring Case, and Considering Multiline Input.)

5

source

The text of the pattern.Updated at the time that the regular expression is created, not executed.

"d(b+)d"

As shown in the second form of this example, you can use the a regular expression created with an object initializer without assigning it to a variable. If you do, however, every occurrence is a new regular expression. For this reason, if you use this form without assigning it to a variable, you cannot subsequently access the properties of that regular expression. For example, assume you have this script:

<script LANGUAGE="JavaScript1.2">
myRe=/d(b+)d/g;
myArray = myRe.exec("cdbbdbsbz");
document.writeln("The value of lastIndex is " + myRe.lastIndex);
</script>

This script displays:

The value of lastIndex is 5
However, if you have this script:
<script LANGUAGE="JavaScript1.2">
myArray = /d(b+)d/g.exec("cdbbdbsbz");
document.writeln("The value of lastIndex is " + /d(b+)d/g.lastIndex);
</script>

It displays:

The value of lastIndex is 0
The occurrences of
/d(b+)d/g in the two statements are
different regular expression objects and hence have different values for their lastIndex property.
If you need to access the properties of a regular expression created with an object initializer, you should first assign it to a variable.

No comments :