JavaScript
  Home arrow JavaScript arrow Page 3 - Understanding the JavaScript RegExp Ob...
FaxWave - Free Trial.
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

    Virtual Tradeshows by Ziff Davis Enterprise - A Unique Opportunity to Connect with IT Experts, Access Information, and Gain Insight on today's Technology

    Understanding the JavaScript RegExp Object - Two to Tango
    (Page 3 of 11 )

    Now that you know what a regular expression is, let's look at using it in a script. JavaScript's String object exposes a number of methods that support regular expressions. The first of these is the search() method, used to search a string for a match to the supplied regular expression. Take a look at the next example:


    <script language="JavaScript">
     
    // define string to be searched
    var str = "The Matrix";
     
    // define search pattern
    var pattern = /trinity/;
     
    // search and return result
    if(str.search(pattern) == -1) 
    {
     alert
    ("Sorry, Trinity is not in The Matrix.");
    } else 
    {

     alert
    ("Trinity located in The Matrix at character " 
    str.search(pattern));
    }
     
    </script>

    When you run this script, you should see the following:

    Sorry, Trinity is not in The Matrix.

    The search() method returns the position of the substring matching the regular expression, or -1 if no match exists. In the example above, it is clear that the pattern "trinity" does not exist in the string "The Matrix," hence the error message.

    Now, look what happens when I update the regular expression so that it results in a positive match:


    <script language="JavaScript">
     
    // define string to be searched
    var str = " The Matrix";
     
    // define search pattern
    var pattern = /tri/;
     
    // search and return result
    if(str.search(pattern) == -1) 
    {
     alert
    ("Sorry, Trinity is not in The Matrix.");
    } else 
    {

     alert
    ("Trinity located in The Matrix at character " 
    str.search(pattern));
    }
     
    </script>

    This time round, the JavaScript interpreter will return a match (and the location where it found the match). Here's the output:

    Trinity located in The Matrix at character 7

    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 4 hosted by Hostway