JavaScript
  Home arrow JavaScript arrow Page 4 - 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 - Splitting a String


    (Page 4 of 4 )

    Consider the following available string:

    var availableString = "You need an exercise book or typing sheets of papers and a pen.";

    We have two English language conjunctions in the string, which are "or" and "and." You can split the sentence into three sub-strings, based on the conjunctions. It is possible for you to have an array of the sub-strings that are separated by any of the conjunctions.

    In the array the first element would be "You need an exercise book," the second element would be "typing sheets of papers" and the last element would be "a pen." So, there would be three elements in the array. The conjunctions would be omitted; they would not be in the array. The available string remains unchanged. You need a String split() function and a RegExp object for the conjunctions to achieve this. The following code illustrates this:

    <html>


    <head>

    </head>


    <body>

    <script type = "text/javascript">

    var availableString = "You need an exercise book or typing sheets of papers and a pen.";

    var re = /or|and/;


    var arrayOfStrs = availableString.split(re);


    alert(arrayOfStrs);

    </script>


    </body>


    </html>

    The first statement declares the available string. The next statement forms the RegExp object. The pattern re would match either "or" or "and." You do not need a global flag. The split() method would scan the entire string. It takes the RegExp object as an argument. The split() method returns an array with the sub-strings in the order in which they are in the available string. If there is no sub-string to return, the split method returns the entire available string.

    This is what the specification (JavaScript 1.5) has to say about the String split() method:

    Syntax:

    split([separator][, limit])

    The split method returns the new array.

    When found, separator is removed from the sub-strings and the sub-strings are returned in an array. If separator is omitted, the array contains one element consisting of the entire string. The original (available) string remains unchanged.

    In JavaScript 1.2 or later, split has the following additions:

    • It can take a limit count so that the resulting array does not include trailing empty elements.
    • If you specify LANGUAGE="JavaScript1.2" in the SCRIPT tag, string.split(" ") splits on any run of 1 or more white space characters including spaces, tabs, line feeds, and carriage returns. For this behavior, LANGUAGE="JavaScript1.2" must be specified in the <SCRIPT> tag.

    The limit Parameter

    For our example above, if you want an array with just the first sub-string, then the argument for the limit parameter is 1. If you want an array with the first and second sub-strings (in their order), then the argument for the limit parameter is 2. If you want an array with the first, second and third sub-strings (in their order), then the argument for the limit parameter is 3.

    We have come to the end of this two-part series. As you can see, whenever you have to choose between a RegExp object method and a String regular expression method, you should choose the String method, since the string methods are easier and more convenient to use.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    JAVASCRIPT ARTICLES

    - Using jQuery to Preload Images with CSS and ...
    - Using Client-Side Scripting to Preload Image...
    - Removing Non-Semantic Markup when Preloading...
    - Using the Display CSS Property to Preload Im...
    - Preloading Images with CSS and JavaScript
    - Scaling and Moving Web Page Elements with th...
    - Fading, Hiding and Sliding HTML Elements wit...
    - Toggling CSS Properties with the GX JavaScri...
    - Cancel, Queue and Pause Animations with the ...
    - Producing Elastic Effects with the GX JavaSc...
    - Moving Divs Diagonally with the GX JavaScrip...
    - Moving Elements Vertically and Horizontally ...
    - Making Bouncing Effects in Parallel with the...
    - Creating Bouncing Effects with the GX JavaSc...
    - Manipulating Background Colors with the GX J...







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