JavaScript
  Home arrow JavaScript arrow Case and Negation in JavaScript Regular Ex...
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

Case and Negation in JavaScript Regular Expressions
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-07-22

    Table of Contents:
  • Case and Negation in JavaScript Regular Expressions
  • Abbreviations for Common Character Classes
  • Beginning and Ending a String
  • Matching Repetitions
  • Matching the Whole String

  • 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


    Case and Negation in JavaScript Regular Expressions


    (Page 1 of 5 )

    Welcome to the third part of a five-part series that examines the use of regular expressions in JavaScript. In this part, we'll look at the effects of case sensitivity, how to use negation, and more.

    Case Sensitivity for Individual Characters

    The following will not produce a match:

     

    /Dog/.test("I have a dog")

     

    while the following produces a match:

     

    /DOg/i.test("I have a dog")

     

    A match is produced in the second case because of the i flag at the end of the regexp.

    With the i flag, you make each of the characters in the regexp case insensitive. There may come a time when you want to make only a particular character in the regexp case insensitive. The solution is to put both the upper and lowercase characters in a class (square brackets).

    Consider the following two available strings:

     

    var availablestring = "He said, Yes"

     

    and

     

    var availablestring = "He said, 'yes'"

     

    Imagine that the above available strings were typed by two different users of your web page, at different times. You might want to know if the word "yes" was typed. The first user typed "yes" beginning with a capital letter; the second user typed it in quotes, with all letters in lowercase. You might want to know if the word "yes" was typed, independent of whether it began with "Y" or "y." The following code (solution) will produce a match:

     

    /[Yy]es/.test(availablestring)

     

    Negation

    Character ranges and some special regexp characters can be negated.

    Any character except a digit is written as

     

    [^0-9]

     

    This refers to all characters that are not in the range 0-9. The following code produces a match:

     

    /[^0-9]/.test("12P34")

     

    P is not in the range [0-9]; P is outside. Concerning all characters, P is in the range [^0-9]. Note the presence and absence of the '^' character between the classes [0-9] and [^0-9].

    The special character used for negation is "^".

    The range outside [a-z] is [^a-z]. That is [^a-z] is the negation of [a-z].

    The range outside [A-Z] is [^A-Z]. That is [^A-Z] is the negation of [A-Z].

    We shall see other negations below.

    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