JavaScript
  Home arrow JavaScript arrow Replacing and Spliting JavaScript Sub Stri...
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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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

Replacing and Spliting JavaScript Sub Strings
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-10-01

    Table of Contents:
  • Replacing and Spliting JavaScript Sub Strings
  • Extracting Continued
  • Search and Replace a Matched sub-String
  • Splitting a String

  • 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
     
     
    ADVERTISEMENT


    Replacing and Spliting JavaScript Sub Strings


    (Page 1 of 4 )

    In this second part of a two-part series on JavaScript String regular expressions, I look at multiline matching, how to replace matched sub-strings and how to split an available string using a regular expression object. Before we do that, let us look at the String way of extracting matches and then compare it with that of the RegExp object.

    Extracting matches with the String match() method.

    Note: The JavaScript String object has the match() method; it is not the RegExp object that has the method. This method returns an array of one element (the first) if it sees a match. If you use a global flag, then the elements in the array will be all the sub-strings found. The way the String match() method is used is straightforward. There is no ambiguity, as we shall see below.

    Consider the regexp,

    re = /rat/;

    Consider the available string,

    "A cat is an animal. A rat is an animal. A bat is an animal."

    The pattern should match "rat" in the available string. The alert statement in the following code displays a one-element array, with one value, "rat."

    <html>


    <head>

    </head>


    <body>

    <script type="text/javascript">

    var availableString = "A cat is an animal. A rat is an animal. A bat is an animal.";

    var re = /cat/;


    myArray = availableString.match(re);

    alert(myArray);

    </script>

    </body>


    </html>


    We have used the string match method in the statement:

    myArray = availableString.match(re);

    Only one match could occur in the available string; only one element was matched, therefore the returned array has only one element, "rat" as expected. If no matching occurs, null is returned; an array is not returned; the same is true with the exec() method.

    More JavaScript Articles
    More By Chrysanthus Forcha


     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek