Home arrow JavaScript arrow Page 3 - Regular expressions in JavaScript
JAVASCRIPT

Regular expressions in JavaScript


JavaScript is useful for a lot more than opening pop-ups. If you use HTML forms on your website, and want to make sure that your visitors submit valid data on those forms, you might want to consider using some regular expressions in JavaScript. Alejandro Gervasio explains how, with many excellent examples.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 248
December 20, 2004
TABLE OF CONTENTS:
  1. · Regular expressions in JavaScript
  2. · The Basics
  3. · Character Escaping
  4. · Repetition
  5. · Counted Subexpressions
  6. · Using regular expressions in JavaScript
  7. · The match() method
  8. · The replace() method
  9. · The test() method

print this article
SEARCH DEVARTICLES

Regular expressions in JavaScript - Character Escaping
(Page 3 of 9 )

Sometimes, meta-meaning characters, such as (^) or ($) and other special ones need to be included within the string to be searched for, representing the corresponding character instead of having the special meaning in the context of regular expressions syntax. To do so, we need to escape them properly in the string, with a backslash. If a backslash has to be represented too, it must be escaped with another backslash (two slashes \\).

Let’s see it in action:

\^ is used to mark the beginning of the string   // Matches any string with a caret (^) in it.

\$ is used to mark the end of the string   // Matches any string with the dollar sign ($) in it.

Character Sets

Anything enclosed in the special square brace brackets [ and ] is a character class, a set of characters to which a matched character must belong. Please note that the expression in the square brackets matches only a simple character.

We can list a set, such as:

[aeiou]

which means any vowel.

Or something like this:

[12345]

which matches “1” and “3” but not “a” or “6”.

We can also describe a range, or set of ranges with the special hyphen character:

[1-5]   // Same as previous example.

[a-z]   // Matches any lowercase letter.

[a-zA-Z]   // Matches  any alphabetic character in lowercase or uppercase.

[0-9a-zA-Z]   // Matches any letter or digit.

Besides, we can use sets to specify that a character cannot be a member of a set.

For example:

[^a-z]   // Matches any character that is not between a and z.

The caret symbol means "not" when it is placed inside the square brackets. As we have seen previously, it has a different meaning when it’s used outside, anchoring the beginning of a string.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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