Home arrow JavaScript arrow Page 5 - 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 - Counted Subexpressions
(Page 5 of 9 )

We can specify how many times something can be repeated by using a numerical expression in curly braces ({ }). We can define an exact number of repetitions ({3} means exactly 3 repetitions), a range of repetitions ({2,4} means from 2 to 4 repetitions), or an open-ended range of repetitions ({2,} means at least two repetitions).

For example,

computer{1,3}   // Matches “computer”, “computer computer” and “computer computer computer”.

Branching

Another useful option in building regular expressions is to represent choices for a string. This is done with a vertical pipe (|).

For example, if we want to match several domains, such as com, edu or net, the following expression would  be used:

( com)|(edu)|(net)

Summary of special characters

Here are a few special characters that can be used for matching characters in regular expressions:

\n     // a newline character

.       // any character except a newline

\r      // a carriage return character

\t      // a tab character

\b     // a word boundary (the start or end of a word)

\B    // anything but a word boundary.

\d     // any digit (same as [0-9])

\D    // anything but a digit (same as [^0-9])

\s     // single whitespace (space, tab, newline, etc.)

\S    // single nonwhitespace.

\w    // A “word character” (same as [0-9a-zA-Z_])

\W   // A “nonword character (same as [^0-9a-zA-Z_]

Of course, there are more special characters and tips for regular expressions, generally well covered in any complete reference. For the sake of brevity, this list is good enough for this article. Since JavaScript has the same support that Perl for regular expressions, any full guide focused on Perl regular expressions will be applicable to JavaScript too.
  
Now, with all of the basics covered, we’ll see how we can add the power of regular expressions to our JavaScript code, making our developer life a lot easier and expanding our background a little bit more.


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