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

In Splits - 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



The String object also comes with a split() method, which can be used to decompose a single string into separate units on the basis of a particular separator value; these units are then placed into an array for further processing. Consider the following example, which demonstrates:

<script language="Javascript">
// set string
var friends = "Joey, Rachel, Monica, Chandler, Ross, 
// split into array using commas
var arr = friends.split("
// iterate through array and print each value
for (x=0; x<arr.length; 
Hiya" + arr[x]);

Up until JavaScript 1.1, you could only use string values as separators. JavaScript 1.2 changed all that; now, you can even split a string on the basis of a regular expression.

To understand this better, consider the following string, which illustrates a common problem: unequal whitespace between separated values:

NeoTrinity   |Morpheus    |  
Smith|  Tank

Here, the | character is used to separate the various names. However, the space between the various | is unequal - which means that before you can use the individual elements of the string, you will need to trim the additional space around them. Splitting by using a regular expression as the separator is an elegant solution to the problem - as you can see from the updated listing below:

<script language="JavaScript">

// define string
var str = "Neo| Trinity   
|Morpheus    |  Smith|  Tank";
// define pattern
var pattern = /s*|s*/;
// split the string using the regular expression as the separator
result = 
// iterate over result array
for(i = 0; i < result.length; i++) 
Character #" + (i+1) + ": " + result[i]);

The output of the call to split() above will be an array containing the names, without any leading or trailing spaces.

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