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

Changing Things Around - 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



You may have noticed from the previous examples that when using a RegExp object, you have to specify the regular expression at the time of constructing the object. So you might be wondering to yourself, what happens if I need to change the pattern at a later time?

Well, the guys at JavaScript HQ have you covered. The compile() method allows a user to update the regular expression used by the RegExp object in its searches. Take a look:

<script language="JavaScript">
// define string
var str = "The Matrix";
// define pattern
var pattern = "trinity"
// define object
var character = new RegExp(pattern);
// look for match
if(character.test(str)) {
alert("Looking for " + 
pattern "...User located in The Matrix");
} else {
("Looking for " 
pattern "...Sorry, user is not in The Matrix");
// change the pattern associated with the RegExp object
var pattern = 
// look for match and display result
("Looking for " pattern "...User located in The Matrix");

else {

("Looking for " pattern "...Sorry, user is not in The 

Notice the use of the compile() method to dynamically update the pattern associated with the RegExp 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: