JavaScript
  Home arrow JavaScript arrow Regular expressions in JavaScript
IBM Developerworks
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  
Download TestComplete 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Web Buyers Guide
 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 / 129
    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
     
     
    Iron Speed
     
    ADVERTISEMENT

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Regular expressions in JavaScript
    (Page 1 of 9 )

    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.

    Introduction

    If you’ve ever programmed in Perl, or have had in your hands a UNIX system, then maybe you are pretty familiar with what regular expressions are. If you think that JavaScript is not well suited for your client-side Web programming needs, only useful to open pop-ups and other fancy windowing-related stuff, you don’t know the whole story. JavaScript includes full support for Perl-style regular expressions, and  it's extremely useful for string matching processes, as I will try to demonstrate in the following lines.

    I’ll start out explaining what, exactly, regular expressions are, and what they can do for you when used in JavaScript. Finally, I’ll show some practical examples, giving a quick taste of how powerful they can be when they are utilized for client-side data validation. Just keep reading!

    What are regular expressions?

    One of the most common situations that come up is having an HTML form for users to enter data. Normally, we might be interested in the visitor’s name, phone number and email address, and so forth. However, even if we are very careful about putting some hints next to each required field, some visitors are going to get it wrong, either accidentally or for malicious purposes.

    Coding a script to check sensitive user data is sometimes pretty straightforward. But most of the time this process is not so easy. With current websites, we’ll need to check the proper standardized format of an email address or a URL. That would be a nightmare for programmers, not to mention a confusing and inefficient error-prone process for checking data validity.

    Here’s where regular expressions come in handy. A regular expression is a way of describing a pattern in a piece of text. In fact, it’s an easy way of matching a string to a pattern. We could write a simple regular expression and use it to check, quickly, whether or not any given string is a properly formatted user input. This saves us from difficulties and allows us to write clean and tight code. Let’s see in detail how to work with regular expressions.

    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

    - 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
    - Adding Zoom Controls to Yahoo! Maps
    - Working with Yahoo! Maps
    - Building Image Zooming Controls with the DOM...
    - Working with Multiple Graphics for a Zoom Ap...
    - Improving an Image Zooming Application with ...
    - Zooming in on Images with JavaScript
    - JavaScript Date Objects: Universal Coordinat...
    - Javascript Objects: More Date Methods
    - JavaScript Objects: Dates
    - JavaScript Objects: Finishing Strings

    Iron Speed

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     





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