View Single Post
  #22 (permalink)  
Old June 8th, 2005, 02:56 PM
AGS AGS is offline
Wrox Technical Editor
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2005
Location: Russia
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think there is misconception in RegExp exec() method’s examples on pages 194-195.
page 194
 
Quote:
quote:The RegExp exec() method, which takes a string as an argument, returns an Array containing all
Quote:

matches
. Consider the following example:
and further there are examples with and without option g , and finally (on page 195) with gi options applied:
Code:
var sToMatch = “a bat, a Cat, a fAt baT, a faT cat”;
var reAt = /at/gi;
var arrMatches = reAt.exec(sToMatch);
and a table with contents of resulting arrMatches array values.

I’ve tried to reproduce these examples, but alas - arrMatches length property always gave me 0. At last (after several hours :)) I’ve found an explanation: exec() method returns an array but only first element contains current match, elements with indexes from 1 to n contain stored backreferences (if any).
http://developer-test.mozilla.org/en...ts:RegExp:exec
So the following paragraphs on page 195 are not correct regarding exec() and match() functional similarity
 
Quote:
quote:The String object has a method called match(), which intentionally mirrors the functionality of the
Quote:

RegExp object’s exec() method
. The main difference is that the method is called on the String object
and the RegExp object is passed in as an argument:
Code:
var sToMatch = “a bat, a Cat, a fAt baT, a faT cat”;
var reAt = /at/gi;
var arrMatches = sToMatch.match(reAt);
This code yields the same result as the previous example, with arrMatches containing all the same
items
.
because match() does return an array containing all matches.
And there is inconsistency with page 213 where there is a correct example of exec() method functionality.

Best regards,
Alexei


Reply With Quote