Home arrow JavaScript arrow Page 7 - Understanding the JavaScript RegExp Object

Objects in the Rear-View Mirror - JavaScript

Need to match and replace patterns on a Web page? You don't need Perl or PHP - JavaScript can do the job just as well. In this article, find out how, with an introduction to the JavaScript RegExp object and its methods. After reading this tutorial, I'm pretty sure you're going to look at JavaScript in a different light. The language ins't the one most commonly associated with image swaps and browser detection, but it serves as a powerful tool to help you execute pattern-matching tasks in the client quickly and efficiently.

  1. Understanding the JavaScript RegExp Object
  2. Enter the Matrix
  3. Two to Tango
  4. Game, Set, Match
  5. Search and Destroy
  6. In Splits
  7. Objects in the Rear-View Mirror
  8. One Mississippi, Two Mississippi...
  9. Changing Things Around
  10. Working with Forms
  11. Over And Out
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 144
February 09, 2004

print this article



So far, all the examples in this article have piggybacked on the String object to demonstrate the power of the regex implementation in JavaScript. But JavaScript also comes with a core JavaScript object, the RegExp object, whose sole raison d'etre is to match patterns in strings and variables.

This RegExp object comes with three useful methods. Take a look:

test() - test a string for a match to a pattern

exec() - returns an array of the matches found in the string, and also permits advanced regex manipulation

compile() - alter the regular expression associated with a RegExp object

Let's look at a simple example:

<script language="JavaScript">
// define str
var str = "The Matrix";
// define RegExp object
var character = new RegExp("tri");
// search for pattern in string
if(character.test(str)) {
located in The Matrix.");
} else {
Sorryuser is not in The 

This is similar to one of the very first examples in this tutorial. However, as you can see, I've adopted a completely different approach here.

The primary difference here lies in my creation of a RegExp object for my regular expression search. This is accomplished with the "new" keyword, followed by a call to the object constructor. By definition, this constructor takes two parameters: the pattern to be searched for, and modifiers if any (I've conveniently skipped these in the example above).
Once the RegExp object has been created, the next step is to use it. Here, I've used the test() method to look for a match to the pattern. By default, this method accepts a string variable as a parameter and compares it against the pattern passed to the RegExp object constructor. If it finds a match, it returns true; if it does not, it returns false. Obviously, this is a more logical implementation than the search() feature of the String object.

>>> More JavaScript Articles          >>> More By Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Javascript for Beginners: An Introduction
- Introduction to JavaScript
- Adding Elements to a Tree with TreeView jQue...
- Using the Persist Argument in a TreeView jQu...
- Using Unique and Toggle in a TreeView jQuery...
- Using Event Delegation for Mouseover Events ...
- Using the Animate Option in a Treeview jQuer...
- Using HTML Lists with Event Delegation in Ja...
- Opened and Closed Branches on a TreeView jQu...
- Mouseover Events and Event Delegation in Jav...
- Creating a TreeView JQuery Hierarchical Navi...
- Event Delegation in JavaScript
- A Look at the New YUI Carousel Control
- Working with Draggable Elements and Transpar...
- Displaying Pinned Handles with Resizable Con...

Developer Shed Affiliates


Dev Shed Tutorial Topics: