JavaScript
  Home arrow JavaScript arrow Metacharacters, Flags and Regular Expressi...
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

Metacharacters, Flags and Regular Expressions in JavaScript
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0
    2009-07-29

    Table of Contents:
  • Metacharacters, Flags and Regular Expressions in JavaScript
  • Metacharacters
  • The lastIndex Property of the Regexp Object
  • Regexps Flags Revisited
  • The m Flag

  • 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


    Metacharacters, Flags and Regular Expressions in JavaScript


    (Page 1 of 5 )

    Welcome to the fourth part of a five-part series on regular expressions in JavaScript. In this part, you'll learn about metacharacters, combining matching features, and more.

    Matching Alternatives

    We can match different character strings with the alternation metacharacter "|." To match "dog" or "cat," we form the regexp /cat|dog/ . JavaScript will first try to match the first alternative, "cat." If cat doesn't match, JavaScript will then try the next alternative, "dog." If "dog" doesn't match either, then the match fails (the test() method returns false).

    Some examples

    The following produces a match:


    /cat|dog|bird/.test("cats are a group of animals")


    Here, "cat" is matched. There is no "dog" or "bird" in the available string.

    Note that in the available string, it is the group of letters, "c," "a" and "t" that is matched. It is not "cats" that is matched. There is no "s" after “cat” in the regexp; "cat" is a string among all the characters in the available string that is matched. Also note that it is not a word that is matched, but a sub-string. Note as well that the space in the available string is a character, which could be a member of a string subset. What I have just said applies to all other matching, not only alternatives.

    The following produces a match:


    /cat|dog|bird/.test("dogs are a group of animals")


    Here "dog" is matched. There is no "cat" or "bird" in the available string. The search did not see "cat," so it matched "dog."

    The following produces a match:


    /cat|dog|bird/.test("birds are a group of animals")


    Here "bird" is matched. There is no "cat" or "dog" in the available string. The search did not see "cat" or "dog," so it matched "bird."

    Now, in the following expression, "cat" and not "dog" is matched.


    /cat|dog|bird/.test("cats and dogs are groups of animals")


    This is because "cat" appears first in the available string, before "dog."

    Also, in the following expression "cat" and not "dog" is matched.


    /dog|cat|bird/.test("cats and dogs are groups of animals")


    Despite the fact that "dog" is the first alternative in the regexp, "cat" appears first in the available string, before "dog," hence the match.

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek