Home arrow JavaScript arrow Page 2 - Metacharacters, Flags and Regular Expressions in JavaScript
JAVASCRIPT

Metacharacters, Flags and Regular Expressions in JavaScript


Welcome to the fourth part of a five-part series on regular expressions in JavaScript. In this part, you'll learn about metacharacters, combining matching features, and more.

Author Info:
By: Chrysanthus Forcha
Rating:  stars stars stars stars stars / 0
July 29, 2009
TABLE OF CONTENTS:
  1. · Metacharacters, Flags and Regular Expressions in JavaScript
  2. · Metacharacters
  3. · The lastIndex Property of the Regexp Object
  4. · Regexps Flags Revisited
  5. · The m Flag

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Metacharacters, Flags and Regular Expressions in JavaScript - Metacharacters
(Page 2 of 5 )

Not all characters can be used "as is" in a match. Some characters, called metacharacters, are reserved for use in regexp notation. The metacharacters are


{} [] () ^ $ . | * + ? /


These characters are used to give meaning to the regexp object.

A metacharacter can be matched by putting a backslash before it. Here are some examples:


/2+2/.test("2+2=4") // doesn't match, '+' is a metacharacter

/2+2/.test("2+2=4") // matches because '+' is treated like an ordinary '+'

/C:WIN/.test("C:WIN32") // matches (MS DOS path)

//usr/bin/perl/.test("/usr/bin/perl") // matches


Combining Matching Features

You can combine matching features. We have seen some of these, such as in /[cbr]at/. Here are more examples:


/[a-z]+s+d*/.test(available string)


This matches a lowercase word, at least some space, and any number of digits.


/(w+)s+1/.test(availablestring)


This matches doubled words of arbitrary length.


/d{2,4}/.test(year)


This is to verify that the year is at least two but not more than four digits.



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