JavaScript
  Home arrow JavaScript arrow Range of Characters and More Regular Expre...
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

Range of Characters and More Regular Expressions in JavaScript
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2009-07-15

    Table of Contents:
  • Range of Characters and More Regular Expressions in JavaScript
  • Some Special Characters
  • Character Classes
  • Range of Characters

  • 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


    Range of Characters and More Regular Expressions in JavaScript


    (Page 1 of 4 )

    Welcome to the second part of a five-part series on regular expressions in JavaScript. In the first part we explained why you would want to be familiar with regular expressions and began describing them. In this part we'll pick up where we left off, covering special characters, combining with other characters, character classes, and more.

    The m Flag

    This flag indicates whether or not to search in the available string across multiple lines.

    Note: the g and m flags are not used in as straightforward a manner as it appears. We shall see how to use these two flags after we have learned other things concerning JavaScript regexps.

    First Half followed by Second Half of RegExp

    Consider the following:

    var availableString = “JackSprat is part of a sentence”;

    You may be interested in a case where you are looking for the word “Jack” followed (immediately) by the word “Sprat.” The notation for this is:

    x(?=y)

    meaning, match x only if x is followed (immediately) by y. In our case, x is “Jack” and y is “Sprat.” In our case the test() function should not match (look for) “Jack Sprat,” where there is a space between “Jack” and “Sprat.” It should also not match, for example, “Jack with Sprat,” where there is a word with two spaces between “Jack” and “Sprat.” A match should only be produced if the word “Jack” is immediately followed by “Sprat.” The literal text expression for our regexp is:

    /Jack(?=Sprat)/

    In our program we can have,

    var re = /Jack(?=Sprat)/;

    With the above variables, the following expression will return true in a conditional (if statement).

    re.test(availableString)

    The notation for the opposite effect to x(?=y) is:

    x(?!y)

    This means that you should match x only if x is not followed (immediately) by y. So if we have

    var availableString = “Jack Sprat is part of a sentence”;

     

    var re = /Jack(?!=Sprat)/;

    With the above variables, the following expression will return true (produce a match) in a conditional (if statement).

    re.test(availableString)

    Note the space between “Jack” and “Sprat” in the available string. The match occurs because “Jack” is not followed by “Sprat;” it is followed by a space.

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