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

Search and Destroy - 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

The previous set of examples highlighted the search capabilities of the String object. But that's not all! You can also perform a search-and-replace operation with the replace() method, which accepts both a regular expression and the value to replace it with. Here's how:


<script language="JavaScript">
 
// set string
var str = "Welcome to the Matrix, Mr. Anderson";
 
// uncomment to check initial value
// alert(str);
 
// replace a string with another string
// The One turns into Smith
str 
str.replace(/Anderson/,"Smith");
 
// display new string
alert(str)
 
</script>

If you load this example in a browser, you will see that the string "Anderson" has been replaced with the string "Smith". The following output illustrates:

Welcome to the Matrix, Mr. Smith

Remember how I used the "g" modifier to search for multiple instances of a pattern within a string? Take it one step further - you can even use it to replace multiple instances of a pattern within the string:


<script language="JavaScript">
 
// set string
var str = "yo ho ho and a bottle of gum";
 
// returns "yoo hoo hoo and a bottle of gum"
alert(str.replace(/os/g, "oo 
"));
 
</script> 

Here, the \s metacharacter matches the space after "yo" and "ho" and replaces with "oo".

You can also use case-insensitive pattern matching - simply add the "i" modifier (for "insensitive") at the end of the pattern. The next example shows you how:


<script language="JavaScript">
 
// set string
var str = "he He hE HE";
 
// returns ho ho ho ho
alert(str.replace(/he/gi, "ho"));
 
</script> 



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