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

At times, regular expressions can look fairly complex, but when it comes right down to it, they are actually text string themselves.
As we can see, in the following example, we are coding a regular expression that searches for the text “JavaScript” (with no quotes):

JavaScript

Pretty simple, isn’t it? Any string containing the text “JavaScript” matches this regular expression. In fact, this is more than an equal comparison, because it’s matching a string somewhere within another string. It can be anywhere, unless specified otherwise.

But it's not always so simple. As real needs arise, finding more complex string patterns can be a lot more difficult. Special characters might be needed, among other things. So, let’s begin working our way through a few examples to explain the basic regular expression syntax.

Anchoring

It’s very useful to represent that a string must start with a specific character or many of them, and end with another. That’s known as string anchoring. For anchoring strings, a caret (^) may be used to indicate the beginning of a string. A dollar sign ($) is used to indicate the end.

For example:

JavaScript    // Matches  “JavaScript is great”, “The power of JavaScript” and “What is JavaScript?”

Using anchoring characters:

^JavaScript   // Matches  “JavaScript shines”, but not “I love JavaScript”

JavaScript$   // Matches “I like JavaScript”, but certainly not “JavaScript is powerful”

^JavaScript$   // matches only “JavaScript” and nothing else.


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