Home arrow JavaScript arrow Page 2 - 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 - Abbreviations for Common Character Classes
(Page 2 of 5 )

d

d stands for any digit, and it abbreviates [0-9]. The following code produces a match:

 

/IDdid/.test("ID5id is an ID")

 

Negated d

D is negated d. It represents any character that is not a digit; that is, [^0-9].

 

s

trnf are white space characters. ' ' or simply ' ' is produced when you press the spacebar of your keyboard. t is produced when you press the tab key on your keyboard. r is the carriage return character. n is the new line character and f is the form feed character. 

s is the abbreviation for any white space character. That is s is equivalent to [ trnf].  

The following code produces a match:

 

var alertMsg = "The first line.rnThe second line."

 

/n/.test(alertMsg)

 

The following code also produces a match:

 

/s/.test(alertMsg)

 

Negated s

S

S is negated s. It represents any character that is not a white space, that is [^s]. 

S, [^s] and [^ trnf] are equivalent.  

The negation symbol negates the class within the square brackets.

 

w

This is a word character. It represents any alphanumeric character, including the underscore. w and [0-9a-zA-Z_] are equivalent.

 

Negated w

W is negated w. It represents any non-word character. W and [^w] are equivalent.

 

The Period '.'

The period '.' matches any character except n. For example, /.s/ matches 'is' in the available string "An apple is on the tree." /.s/ represents two characters, that is any character (except n) followed by 's'.

The dswDSW and dot abbreviations can be used both inside and outside of character classes. Here are some in use: 

/dd:dd:dd/ : matches an hh:mm:ss time format, e.g 05:45:30.

/[ds]/ : matches any digit or any white space character.

/wWw/ : matches a word char, followed by a non-word char, followed by a word char.

/..rt/ : matches any two chars, followed by 'rt'

/end./ : matches 'end.'

/end[.]/ : also matches 'end.' 


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