JavaScript
  Home arrow JavaScript arrow Page 6 - 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 
 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Understanding the JavaScript RegExp Object - In Splits
    (Page 6 of 11 )

    The String object also comes with a split() method, which can be used to decompose a single string into separate units on the basis of a particular separator value; these units are then placed into an array for further processing. Consider the following example, which demonstrates:


    <script language="Javascript">
     
    // set string
    var friends = "Joey, Rachel, Monica, Chandler, Ross, 
    Phoebe";
     
    // split into array using commas
    var arr = friends.split("
    ");
     
    // iterate through array and print each value
    for (x=0; x<arr.length; 
    x++)
    {
     alert("
    Hiya" + arr[x]);
    }
     
    </script>

    Up until JavaScript 1.1, you could only use string values as separators. JavaScript 1.2 changed all that; now, you can even split a string on the basis of a regular expression.

    To understand this better, consider the following string, which illustrates a common problem: unequal whitespace between separated values:


    NeoTrinity   |Morpheus    |  
    Smith|  Tank

    Here, the | character is used to separate the various names. However, the space between the various | is unequal - which means that before you can use the individual elements of the string, you will need to trim the additional space around them. Splitting by using a regular expression as the separator is an elegant solution to the problem - as you can see from the updated listing below:


    <script language="JavaScript">

    // define string
    var str = "Neo| Trinity   
    |Morpheus    |  Smith|  Tank";
     
    // define pattern
    var pattern = /s*|s*/;
     
    // split the string using the regular expression as the separator
    result = 
    str.split(pattern);
     
    // iterate over result array
    for(i = 0; i < result.length; i++) 
    {
     alert("
    Character #" + (i+1) + ": " + result[i]);
    }
     
    </script>

    The output of the call to split() above will be an array containing the names, without any leading or trailing spaces.

    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