JavaScript
  Home arrow JavaScript arrow Page 8 - Understanding the JavaScript RegExp Ob...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Understanding the JavaScript RegExp Object
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 136
    2004-02-09

    Table of Contents:
  • Understanding the JavaScript RegExp Object
  • Enter the Matrix
  • Two to Tango
  • Game, Set, Match
  • Search and Destroy
  • In Splits
  • Objects in the Rear-View Mirror
  • One Mississippi, Two Mississippi...
  • Changing Things Around
  • Working with Forms
  • Over And Out

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Understanding the JavaScript RegExp Object - One Mississippi, Two Mississippi...
    (Page 8 of 11 )

    The next method I'll show you is the  exec() method. The behavior of this method is similar to that of the String object's match() method. Take a look:


    <script language="JavaScript">
     
    // define string
    var place = "Mississippi";
     
    // define pattern
    var obj = /is./;
     
    // search for match
    // place result in array
    result = 
    obj.exec(place);
     
    // display result
    if(result != null) {
     alert("Found " + result[0] 
    " at " result.index);
    }
    </script>
     

    The exec() method returns a match to the supplied regular expression, if one exists, as an array; you can access the first element of the array to retrieve the matching substring, and the location of that substring with the index() method.

    The main difference between the match() and exec() methods lies in the parameters passed. The former requires a pattern as argument, while the latter requires the string variable to be tested.

    However, that's not all. The exec() method has the ability to continue searching within the string for the same pattern without requiring you to use the "g" modifier. Let me tweak the above example to demonstrate this feature:


    <script language="JavaScript">
     
    // define string
    var place = "Mississippi";
     
    // define pattern
    var obj = /is./;
     
    // search for all matches in string
    // display result
    while((result = 
    obj.exec(place)) != null) {
     alert
    ("Found " result[0] + " at " 
    result.index);
    }
     
    </script>

    So what do we have here? For starters, I have used a "while" loop to call the exec() method repeatedly, until it reaches the end of the string (at which point the object will return null and the loop will terminate). This is possible because every time you call exec(), the RegExp object continue to search from where it left off in the previous iteration.

    At least that's the theory - the code above doesn't work as advertised in either Internet Explorer or Netscape Navigator, so you should be careful when using it. Consider the above a purely theoretical example, then... at least until the browser makers fix the bug.

    Another interesting point to note in the example above is my definition of the RegExp object. Unlike the previous example, you will notice that I have not used the constructor or the "new" keyword to create the object; instead, I've simply assigned the pattern to a variable. Think of this as a shortcut technique for creating a new RegExp object.

    More JavaScript Articles
    More By Harish Kamath, (c) Melonfire


     

       

    JAVASCRIPT ARTICLES

    - Getting Attention with Interactive Effects
    - Interacting with Tooltips and Previews
    - Just-in-Time Information and Ajax
    - Interactive Effects
    - Using Cookies With JavaScript
    - Understanding the JavaScript RegExp Object
    - Controlling Browser Properties with JavaScri...
    - Using Timers in JavaScript
    - Form Validation with JavaScript
    - JavaScript Exception Handling
    - Stringing Things Along
    - Understanding The JavaScript Event Model (pa...
    - Understanding The JavaScript Event Model (pa...
    - An Object Lesson In JavaScript

    Iron Speed



    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway