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

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.


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