SunQuest
 
       JavaScript
  Home arrow JavaScript arrow Page 2 - JavaScript Objects: Finishing Strings
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
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

JavaScript Objects: Finishing Strings
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-01-28

    Table of Contents:
  • JavaScript Objects: Finishing Strings
  • The Replace() Method
  • The Search() Method
  • The Slice() Method
  • The Strike(), Sub(), Small() and Sup() Methods
  • The ToLowerCase() and ToUpperCase Methods

  • 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

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    JavaScript Objects: Finishing Strings - The Replace() Method


    (Page 2 of 6 )

    Suppose you created a string containing the sentence "I love my girlfriend Moniqua." When you guys break up, you may want to change that name to the name of your new girlfriend. You could use the Replace() method to do just that:


    <html>

    <body>


    <script type="text/javascript">


    var girlfriend="I love Moniqua!"

    document.write(girlfriend.replace(/Moniqua/,"Jolie"))


    </script>

    </body>

    </html>

    The result of the above code would be:

      I love Jolie!

    All we did was create a variable named girlfriend, and assign the value “I love Moniqua!” to it. Next, after having broken up with Moniqua for having a really dumb name, we used the Replace() method to swap out Moniqua for Jolie. Take that Brad Pitt!

    You will note that the Replace() method is case-sensitive. If you want to do a replace and not have to worry about whether or not the string is upper or lower case, you can do this:


    <html>

    <body>


    <script type="text/javascript">


    var girlfriend="I love Moniqua!"

    document.write(girlfriend.replace(/moniqua/i,"Jolie"))


    </script>

    </body>

    </html>

    It's the same exact code, only this time we added an “i” after moniqua and before the comma seperator.

    It results in the same thing of course:

      I love Jolie!

    Say you have multiple words to replace. Maybe you wrote a whole poem for Moniqua, and being the romantic guy that you are, you've decided to delete her name and insert Jolie's name in its place. How thoughtful.


    <html>

    <body>


    <script type="text/javascript">


    var str="My Dear Moniqua! "

    str=str + "I love thee Moniqua, "

    str=str + "The way you love Pork Chops; "

    str=str + "Like your cholosterol filled veins, "

    str=str + "Everytime I see you my heart stops. "

    str=str + "<br />Moniqua, Moniqua, Moniqua"

    document.write(str.replace(/Moniqua/g, "Jolie"))


    </script>


    </body>

    </html>

    Pretty simple code right? You will notice here that we used the “g” after the name Moniqua to signify a Global Replace. This replaces all of the Moniquas in the program with Jolies. Here is the result:

      My Dear Jolie! I love thee Jolie, The way you love Pork Chops; Like your cholosterol filled veins, Everytime I see you my heart stops.
      Jolie, Jolie, Jolie

    Again note that the function is case-sensitive. So if we had used a lower case moniqua, nothing would have changed.

    To remedy this matter, we simply add an “i” as before:


    <html>

    <body>


    <script type="text/javascript">


    var str="My Dear Moniqua! "

    str=str + "I love thee Moniqua, "

    str=str + "The way you love Pork Chops; "

    str=str + "Like your cholosterol filled veins, "

    str=str + "Everytime I see you my heart stops. "

    str=str + "<br />Moniqua, Moniqua, Moniqua"

    document.write(str.replace(/Moniqua/gi, "Jolie"))


    </script>


    </body>

    </html>

    More JavaScript Articles
    More By James Payne


       · Thanks for stopping by to check out my final article in the Javascript String...
     

    JAVASCRIPT ARTICLES

    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...
    - Adding Pan Controls to Yahoo! Maps
    - Adding Zoom Controls to Yahoo! Maps
    - Working with Yahoo! Maps
    - Building Image Zooming Controls with the DOM...
    - Working with Multiple Graphics for a Zoom Ap...

    Iron Speed

    Iron Speed





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