JavaScript
  Home arrow JavaScript arrow String Objects and Regular Expressions in ...
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

String Objects and Regular Expressions in JavaScript
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2009-08-05

    Table of Contents:
  • String Objects and Regular Expressions in JavaScript
  • Remembering a Match
  • Matching a Sub-String with its Sub-Sets
  • Search and Replace
  • Knowing the Index of the Match

  • 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


    String Objects and Regular Expressions in JavaScript


    (Page 1 of 5 )

    Welcome to the fifth article in a five-part series on JavaScript regular expressions. In this article, we begin the second and final phase of the series. In this phase we shall deal more with the properties and methods of the regexp object. We shall also deal with some properties and methods of the string object that are related to regular expressions.

    Extracting Matches

    Many patterns of regexps are not as simple as the pattern "boy," "cat" or "dog." You will often have a pattern of possible sub-strings in the available string. The question now is that, when a match has occurred, how can you know the exact sub-string in the available string where the match occurred?

    To achieve this, you have to use a method of the regexp object called the exec method. This method returns a result array. The elements of the result array are the possible matches (sub-strings) found in the available string. The use of this exec method is not straightforward, but it is understandable. We shall take it step by step.

    The syntax is:

     

    var myResultArr = regexp.exec(availableString)

     

    or

     

    var myResultArr = regexp(availableString)

     

    myResultArr is the name you give to the return result array. Regexp is the regular expression object e.g. re, or the literal e.g. /[cbr]at/. 

    Before we continue, know that the test function and the exec function are methods of the regular expression object. The test method returns true or false, while the exec function returns a result array.

    If the exec function succeeds (sees a match) it returns the result function. If it fails, it returns null.

    Consider the following script:

     

    <script type = "text/javascript">

    var availableString = "The boy is good";

    var re = /boy/;

     

    var myArray = re.exec(availableString);

     

    alert(myArray.length);

    alert(myArray[0]);

    </script>

     

    The first line is the available string, which is "The boy is good." The second line is the regexp, which is /boy/. The third line is the exec method call. One match occurs, at "boy" in the available string. So, the length of the result array is one. The sub-string "boy" is the only element in the result array.

    Consider the following script:

     

    <script type = "text/javascript">

    var availableString = "The boy is good";

    var re = /girl/;

     

    var myArray = re.exec(availableString);

     

    alert(myArray);

    </script>

     

    The first line is the available string, same as before. The next line is the regexp, which is /girl/. The third line calls the exec method. The sub-string "girl" is not found in the available string, so no match occurs; the exec method returns null. So the value of the result array, myArray, is null. You can try the code.

    Like the test method, the exec method will test only the first possible match in the available string.

    You can use the g flag to test the different possible matches with the exec method, just as we did for the test method. Consider the following script:

     

    <script type = "text/javascript">

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

    var re = /[cbr]at/g;

     

    var myArray = re.exec(availableString);

    alert(myArray);

    alert(re.lastIndex)

     

    var myArray = re.exec(availableString);

    alert(myArray);

    alert(re.lastIndex)

     

    var myArray = re.exec(availableString);

    alert(myArray);

    alert(re.lastIndex)

    </script>

     

    The first line is the available string. It has the sub-strings "cat," "rat" and "bat." The second line is the regexp. It is coded such that if the JavaScript sees "cat," it should match; if it sees "bat," it should match and if it sees "rat," it should match. The regexp has a global flag (g).

    The following code segment should match "cat." The result array will have only one element, "cat." The lastIndex here is 5.

    The following code segment should match "rat." The result array will have only one element, "rat." The lastIndex here is 25.

    The following code segment should match "bat." The result array will have only one element, "bat'."The lastIndex here is 45.

    The above illustration shows how you can use the global flag (g) 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