Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers by Nicholas C. Zakas; ISBN: 9780764579080
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 17th, 2006, 11:22 PM
Registered User
 
Join Date: Dec 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Question on RegExp exec() method

On page 195, after execute the following code
Code:
var sToMatch = "a bat, a Cat, a fAt baT, a faT cat";
var reAt = /at/gi;
var arrMatches = reAt.exec(sToMatch);
you said we get an array contains all the instances of "at" with indexes from 0 to 5. However I tested the code both in Firefox and IE7 and found I couldn't get the value from 1 to 5.

After I read instructions from O'Relly Javascript Reference , I get that the return array contains only index, input and [0] propreties. [1]...[n] is the String of the results of [parenthesized component matches. So with no parenthesized component I only get [0], while a subsequent call to reAt.exec(sToMatch);will continue the search and get the next "at".




 
Old December 17th, 2006, 11:49 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

While I'm not sure of your question, this is an error. The example should be:

Code:
var sToMatch = "a bat, a Cat, a fAt baT, a faT cat";
var reAt = /at/gi;
var arrMatches = sToMatch.match(reAt);
You are right, the exec() method returns an object representing the first match.

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
 
Old June 7th, 2007, 01:19 AM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

followd by your code , only one index can be found
a poor method about do exec() like this:
    var sToMatch = "a bat, a Cat, a fAt baT, a faT cat";
    var reAt = /at/gi;
    var a0 = reAt.exec(sToMatch);
    var a1 = reAt.exec(sToMatch);
    var a2 = reAt.exec(sToMatch);
    var a3 = reAt.exec(sToMatch);
    var a4 = reAt.exec(sToMatch);
    var a5 = reAt.exec(sToMatch);
    var a6 = reAt.exec(sToMatch);
    alert(a0 + a1 + a2 + a3 + a4 + a5 + a6);

but the description from JS Reference 1.5 is :
exec :
Executes the search for a match in a specified string. Returns a result array.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about the clone() method ianni Java Basics 1 December 26th, 2007 05:14 AM
Question about regexp Micheas BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 December 14th, 2007 02:10 PM
SysCmd method question... cjudd VBScript 2 July 22nd, 2005 11:59 AM
Sort Method question rgalehouse Javascript 1 February 10th, 2004 07:03 PM
Question on using the CONFIRM() method savoym Javascript 5 September 11th, 2003 10:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.