Home arrow JavaScript arrow Page 4 - 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 - Range of Characters
(Page 4 of 4 )

The “-“ Character

There may come a time when you want to match any occurrence of a digit between 0 to 9, or a lower case character between "a" and "z," or an uppercase character between A to Z, in an available string. These are ranges of characters, and for each range you would want to know if one character in the range exists in the available string (I will address the issue of multiple occurrences of a character of a range in the available string later).

The “-“ character is used for this. So the range 0 to 9 is denoted by 1-9; ‘a’ to ‘z’ by a-z; and A to Z by A-Z.

The following code produces a match:

 

/[0-9]/.test(“ID5id”)

 

Recall that the square brackets indicate that any element it contains should be tested. So a match occurs between 5 in the range 1 to 9 and 5 in the unavailable string “ID5id.”

The above expression is the same as

 

/[0123456789]/.test(“ID5id”)

 

The following code will produce a match for a similar reason:

 

/[a-z]/.test("ID5i")

 

A match occurs between ‘i’ in the range a-z and ‘i’, the only lowercase letter in our present available string.

Of course you can combine a range with other characters in the regular expressions. The regexps /ID[0-9]id/ will match “ID4id,” “ID5id” and “ID6id”; in fact, it will match any word beginning with "ID" followed by a digit and then "id." So

 

/ID[0-9]id/.test(“ID2id is an ID”)

 

produces a match.

Note: the range format gives a short form of writing a class. It is any one element in the square brackets that is matched.

We have done a lot so far, but there are still many things to be learned. Regular Expressions is a relatively new concept in software programming. So, we shall continue to take it step by step.

We have finished with the idea of range of characters. This is a good place to take a break. We continue in the next part of the series.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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