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

Often, it’s useful to specify that there might be multiple occurrences of a particular string. We can represent this using the following special characters: “?”, “+” and “*”. Specifically, “?” means that the preceding character is optional, “+” means one or more of the previous character, while “*” means zero or more of the previous character.

Let’s see some examples to clarify this concept:

comput?er    // Matches “computer” and “compuer”, but not “computter”.

comput+er   // Matches “computer” and “computter”, but not “compuer”.

comput*er   // Matches “compuer” and “computter” but not “coputer”.

^[a-zA-Z]+$   // Matches any string of one or more letters and nothing else.

Subexpressions

Sometimes, it’s good to be able to split an expression into subexpressions, so, it’s possible, for example, to represent “at least one of these strings followed by exactly one of those.” We can achieve this using parentheses and combinations of the special characters ?,+ and *, exactly as we would do in an arithmetic expression.

For example,

(good)?computer    // Matches “good computer” and “computer”, but not “good good computer”.

(good)+computer   // Matches “good computer” and “good good computer” but not “computer”.

(good)*computer   // Matches “computer” and “good good computer” but not “good computers”.


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