JavaScript
  Home arrow JavaScript arrow Page 6 - 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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 / 178
    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 - Using regular expressions in JavaScript


    (Page 6 of 9 )

    Using regular expressions in JavaScript is very easy, often being passed over by people who don’t know that it can be done, or by developers arguing that parsing regular expressions slows down client-side applications. Whatever the reasons are, let’s show how we can create a regular expression in JavaScript:

    var re = /regexp/;

    where regexp is the regular expression itself. Extending the concept to our first example presented in the basics section, let’s build one that detects the string “JavaScript”:

    var re = /JavaScript/;

    As default behavior, JavaScript regular expressions are case sensitive and only search for the first match in any given string. But we can add more functionality by adding the g and i modifiers (g for global and i for insensitive). Annexing the modifiers after the last /, we can make a regular expression search for all matches in the string and ignore case. Once again, let’s see some examples to properly understand these concepts.

    Given the string “example1 Example2 EXAMPLE3”, the following regular expressions match as listed below:

    /Example[0-9]+/   // Matches “Example2”

    /Example[0-9]+/i   // Matches “example1”

    /Example[0-9]+/gi   // Matches “example1”, “Example2” and “EXAMPLE3”

    As seen in the previous examples, the use of “i” and “g” modifiers increases noticeably the matching capabilities of regular expressions.  So don’t forget they exist when you code your next script.

    Applying methods to JavaScript strings

    Using a regular expression is easy. Every JavaScript variable containing a text string is able to support four main methods (in Object Oriented parlance) or functions to work with regular expressions. They are match(), replace(), search() and test(), the last one being an object method rather than a string method.

    We’ll see in turn how they work.

    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...
       · I just wanted to give you a thumbs up. I needed an email address pattern for a...
       · Thanks for the kind comments on my JavaScript article and it's always good to know...
       · I am a relative novice to regular expressions in JavaScript so excuse my question if...
       · Thanks for commenting on my JavaScript article. Yes, definitively there’s a much...
       · Let me thank you for such a wonderful topic. Because of this I was able to solve...
       · Good to know my article has been useful to you. Regarding your question, you might...
       · Thanks Alejandro Gervasio for your solution.I have one more way to do the same....
       · Good to know you solved your problem. Don't forget to perform strict server-side...
     

    JAVASCRIPT ARTICLES

    - Using jQuery to Preload Images with CSS and ...
    - Using Client-Side Scripting to Preload Image...
    - Removing Non-Semantic Markup when Preloading...
    - Using the Display CSS Property to Preload Im...
    - Preloading Images with CSS and JavaScript
    - Scaling and Moving Web Page Elements with th...
    - Fading, Hiding and Sliding HTML Elements wit...
    - Toggling CSS Properties with the GX JavaScri...
    - Cancel, Queue and Pause Animations with the ...
    - Producing Elastic Effects with the GX JavaSc...
    - Moving Divs Diagonally with the GX JavaScrip...
    - Moving Elements Vertically and Horizontally ...
    - Making Bouncing Effects in Parallel with the...
    - Creating Bouncing Effects with the GX JavaSc...
    - Manipulating Background Colors with the GX J...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek