SunQuest
 
       JavaScript
  Home arrow JavaScript arrow Page 5 - Regular expressions in JavaScript
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Regular expressions in JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 140
    2004-12-20

    Table of Contents:
  • Regular expressions in JavaScript
  • The Basics
  • Character Escaping
  • Repetition
  • Counted Subexpressions
  • Using regular expressions in JavaScript
  • The match() method
  • The replace() method
  • The test() method

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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.

    More JavaScript Articles
    More By Alejandro Gervasio


       · The phone number validation code does not work per the article. Not only that, but...
       · my code did not cut and paste well as you can see above. You may test it out on my...
       · Sorry it took me so long to reply back.Your commnents is always welcome. The...
       · Hi again,I checked your code at your server and it's working just fine....
       · hitiy need to change the quotes from `eg validateEmail( this.email , `Please...
       · Yes, there is an error about the quotes. Due to program incompatibility.Just...
       · Yes, there is an error about the quotes. Due to program incompatibility.Just...
       · In the code a ] was inadvertenly placed instead of a }. Probably a typo - forgot to...
       · Hi Htarko,Thank you for commenting on this article, and of course thank you for...
       · I read your article.. It's fine.. But I couldn't find the regular expression for...
       · Thank you for commenting on my JavaScript article. Regarding your question, you can...
     

    JAVASCRIPT ARTICLES

    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...
    - Adding Pan Controls to Yahoo! Maps







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway