Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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".




Reply With Quote
  #2 (permalink)  
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/
Reply With Quote
  #3 (permalink)  
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:52 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.