Home arrow JavaScript arrow Page 3 - Range of Characters and More Regular Expressions in JavaScript
JAVASCRIPT

Range of Characters and More Regular Expressions in JavaScript


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.

Author Info:
By: Chrysanthus Forcha
Rating: 3 stars3 stars3 stars3 stars3 stars / 2
July 15, 2009
TABLE OF CONTENTS:
  1. · Range of Characters and More Regular Expressions in JavaScript
  2. · Some Special Characters
  3. · Character Classes
  4. · Range of Characters

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Range of Characters and More Regular Expressions in JavaScript - Character Classes
(Page 3 of 4 )

The Square Brackets

A character class allows a set of possible characters, rather than just a single character, to match at a particular point in a regexp. Character classes are denoted by brackets [...], with the set of characters to be possibly matched inside. Here are some examples: 

Let your available string be

 

“He has a cat.”

 

You may know that he has an animal, but it does not matter to you which animal he has. You will be satisfied if he has a cat, bat or a rat. Note that the words, “cat”, “bat” and “rat” each have “at” but begin with a “c” or “b” or “r.” The regexp to check this is

 

/[bcr]at/

 

The following produces a match

 

/[bcr]at /.test(“He has a cat.”)

 

Here, because of the brackets, we interpret the regexp as follows: the test() function should match any word whose first character is a “b,” “c” or “r,” the rest of the characters being "at."

The square brackets denote a class of elements. However, it is any one element in the class that is to be tested, not all of them together.


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 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials