JavaScript
  Home arrow JavaScript arrow Page 3 - 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  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 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


    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.

    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...
       · Hi,With regular expression in javascript, does anybody know how find a match of...
       · Thanks for commenting on my JavaScript article. Regarding your question, there’re...
       · Hi,I'm gettting an error with the line "var strRegex= /^(is)|(in)|(also)+$/;"....
       · Hi Alejandro Gervasio,I've tried your code, but it's not working. Any other...
       · Hi,The above function was only a prototype. As I said before, you should tune...
     

    JAVASCRIPT ARTICLES

    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - 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...







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