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

Two to Tango - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Now that you know what a regular expression is, let's look at using it in a script. JavaScript's String object exposes a number of methods that support regular expressions. The first of these is the search() method, used to search a string for a match to the supplied regular expression. Take a look at the next example:


<script language="JavaScript">
 
// define string to be searched
var str = "The Matrix";
 
// define search pattern
var pattern = /trinity/;
 
// search and return result
if(str.search(pattern) == -1) 
{
 alert
("Sorry, Trinity is not in The Matrix.");
} else 
{

 alert
("Trinity located in The Matrix at character " 
str.search(pattern));
}
 
</script>

When you run this script, you should see the following:

Sorry, Trinity is not in The Matrix.

The search() method returns the position of the substring matching the regular expression, or -1 if no match exists. In the example above, it is clear that the pattern "trinity" does not exist in the string "The Matrix," hence the error message.

Now, look what happens when I update the regular expression so that it results in a positive match:


<script language="JavaScript">
 
// define string to be searched
var str = " The Matrix";
 
// define search pattern
var pattern = /tri/;
 
// search and return result
if(str.search(pattern) == -1) 
{
 alert
("Sorry, Trinity is not in The Matrix.");
} else 
{

 alert
("Trinity located in The Matrix at character " 
str.search(pattern));
}
 
</script>

This time round, the JavaScript interpreter will return a match (and the location where it found the match). Here's the output:

Trinity located in The Matrix at character 7



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVASCRIPT ARTICLES

- 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: