JavaScript
  Home arrow JavaScript arrow Page 7 - Understanding the JavaScript RegExp Ob...
The Best Selling PC Migration Utility.
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 
 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

    Virtual Tradeshows by Ziff Davis Enterprise – A Unique Opportunity to Interact with IT Experts, Access Information, and Gain Insight on Today’s Trends in Technology Learn more

    Understanding the JavaScript RegExp Object - Objects in the Rear-View Mirror
    (Page 7 of 11 )

    So far, all the examples in this article have piggybacked on the String object to demonstrate the power of the regex implementation in JavaScript. But JavaScript also comes with a core JavaScript object, the RegExp object, whose sole raison d'etre is to match patterns in strings and variables.

    This RegExp object comes with three useful methods. Take a look:

    test() - test a string for a match to a pattern

    exec() - returns an array of the matches found in the string, and also permits advanced regex manipulation

    compile() - alter the regular expression associated with a RegExp object

    Let's look at a simple example:


    <script language="JavaScript">
     
    // define str
    var str = "The Matrix";
     
    // define RegExp object
    var character = new RegExp("tri");
     
    // search for pattern in string
    if(character.test(str)) {
    alert("User 
    located in The Matrix.");
    } else {
    alert("
    Sorryuser is not in The 
    Matrix
    .");
    }
     
    </script> 

    This is similar to one of the very first examples in this tutorial. However, as you can see, I've adopted a completely different approach here.

    The primary difference here lies in my creation of a RegExp object for my regular expression search. This is accomplished with the "new" keyword, followed by a call to the object constructor. By definition, this constructor takes two parameters: the pattern to be searched for, and modifiers if any (I've conveniently skipped these in the example above).
     
    Once the RegExp object has been created, the next step is to use it. Here, I've used the test() method to look for a match to the pattern. By default, this method accepts a string variable as a parameter and compares it against the pattern passed to the RegExp object constructor. If it finds a match, it returns true; if it does not, it returns false. Obviously, this is a more logical implementation than the search() feature of the String 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