Home arrow JavaScript arrow Case and Negation in JavaScript Regular Expressions
JAVASCRIPT

Case and Negation in JavaScript Regular Expressions


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.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
July 22, 2009
TABLE OF CONTENTS:
  1. · Case and Negation in JavaScript Regular Expressions
  2. · Abbreviations for Common Character Classes
  3. · Beginning and Ending a String
  4. · Matching Repetitions
  5. · Matching the Whole String

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Case and Negation in JavaScript Regular Expressions
(Page 1 of 5 )

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.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials