JavaScript
  Home arrow JavaScript arrow Page 5 - Understanding the JavaScript RegExp Ob...
CIO Insight
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Understanding the JavaScript RegExp Object - Search and Destroy
    (Page 5 of 11 )

    The previous set of examples highlighted the search capabilities of the String object. But that's not all! You can also perform a search-and-replace operation with the replace() method, which accepts both a regular expression and the value to replace it with. Here's how:


    <script language="JavaScript">
     
    // set string
    var str = "Welcome to the Matrix, Mr. Anderson";
     
    // uncomment to check initial value
    // alert(str);
     
    // replace a string with another string
    // The One turns into Smith
    str 
    str.replace(/Anderson/,"Smith");
     
    // display new string
    alert(str)
     
    </script>

    If you load this example in a browser, you will see that the string "Anderson" has been replaced with the string "Smith". The following output illustrates:

    Welcome to the Matrix, Mr. Smith

    Remember how I used the "g" modifier to search for multiple instances of a pattern within a string? Take it one step further - you can even use it to replace multiple instances of a pattern within the string:


    <script language="JavaScript">
     
    // set string
    var str = "yo ho ho and a bottle of gum";
     
    // returns "yoo hoo hoo and a bottle of gum"
    alert(str.replace(/os/g, "oo 
    "));
     
    </script> 

    Here, the \s metacharacter matches the space after "yo" and "ho" and replaces with "oo".

    You can also use case-insensitive pattern matching - simply add the "i" modifier (for "insensitive") at the end of the pattern. The next example shows you how:


    <script language="JavaScript">
     
    // set string
    var str = "he He hE HE";
     
    // returns ho ho ho ho
    alert(str.replace(/he/gi, "ho"));
     
    </script> 

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