JavaScript
  Home arrow JavaScript arrow Page 6 - Introduction to Regular Expressions in Jav...
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

Introduction to Regular Expressions in JavaScript
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-07-08

    Table of Contents:
  • Introduction to Regular Expressions in JavaScript
  • Simple Word Matching
  • Meaning of Pattern
  • Regular Expression Object
  • Simple Usage of the Literal Text Format and the Constructor Function
  • The Flags

  • 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


    Introduction to Regular Expressions in JavaScript - The Flags


    (Page 6 of 6 )

    You have three possible values:

    g : meaning global match.

    i : meaning ignore case.

    m : meaning match over multiple lines.


    The g Flag

    Consider the following statement:


    var availableString = "I have a dog. You have a dog. She has a dog.";


    Matching normally occurs for the first case in an available string. If your regexp is


    var re = /dog/


    then it is the "dog" in the first sentence that will be matched (seen in the available string). The "dog" in the second and third sentences will not be matched. If you want to match all the cases of the word "dog," then your regexp will have to be


    var re = /dog/g


    The g is a flag that means global match; that is, match all the occurrences of the regexp in the string. The question you may have now is "What do you gain with global matching?" As we go on, you will see the uses. For example, you may want to know the number of occurrences of the regexp in the available string. I will show you how to get this later.

    With the constructor function you would have


    var re = new RegExp("dog","g")

     

    This statement is equivalent to the previous one.


    The i Flag

    By default, matching is case sensitive. To make it case insensitive, you have to use the i flag.


    So if we have


    var re = /send/


    which is equivalent to


    var re = new RegExg("send")


    and then we also have


    var availableString = "Click the Send button."


    The following code will not produce a match:


    <script type="text/javascript">

    var re = /send/;

    var availableString = "Click the Send button.";

     

    if (re.test(availableString))

    alert('Matched')

    else

    alert('Not Matched')

    </script>


    The regexp did not match the available string because the regexp has "send" where s is in lower case, but the available string has "Send" where S is in upper case. If you want this matching to be case insensitive, then your regexp will have to be


    var re = /send/i


    or


    var re = new RegExg("send","i")


    The following code will produce a match.


    <script type="text/javascript">

    var re = new RegExp("send","i");

    var availableString = " Click the Send button.";

     

    if (re.test(availableString))

    alert('Matched')

    else

    alert('Not Matched')

    </script>


    Matching has occurred because we have made the regexp case insensitive, with the i flag.

    We take a break here and continue in the next part of the series.


    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.

       · very nice article....explained in great detail...a newbie can understand it...
       · Hi,Thanks for the comment. Contact me at forchatrans@yahoo.com for additional...
     

    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