spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / professional / chap3 / 6
[next]
Developer News
Metasploit 3.2 Offers More 'Evil Deeds'
'Thank You Apple. Seriously.'
The Buzz: BlackBerry App Store Seen Next

Professional JavaScript

RE Methods and Properties

Now you know how to write your very own patterns, what do you do with them? Well, we've already discovered that there is a RegExp object that is automatically created when you assign a string pattern to a variable. Now it's time to meet the methods we use to do our dirty work with. There are two attached to the RegExp object and four to the String object that work with REs, as follows.

str.search()

The String object's search() method is the simplest of all the operations we cover here.


var str="96521234";
var reg= new RegExp("965");
var reg2= /123/;
var index=str.search(reg);              // index = 0
var index2=str.search (reg2);            // index2 = 4

As you can guess from our example, search simply looks through the String specified and returns to index the position of the first matching character sequence in the String. Note that String ignores the global switch 'g' in a RE literal and that this index value begins at 0 as line 4 demonstrates. If a match is not found, search returns -1.

str.split()

The split() method has been in the language since JavaScript 1.1, but with version 1.2 came support for it to take a regular expression argument.


var large_number = "212,0,456,0,67889";
var reg3 = /,\d,/;
var numberList = large_number.split(re);
                                        // numberList = ["212","456","67889"]

The purpose of split() is to take a string and return an array of string elements. Each element exists in the original string separated by characters matching the pattern sent to split() as its argument.

str.replace()

As you would imagine, the replace() method returns a brand new string that contains a copy of the original string with any matching part of it replaced accordingly. For example


reg4 = /happy/gi;
str4 = "I'm happy. You're Happy";
newstr4=str4.replace(reg4, "sad");   // newstr4 = "I'm sad. You're sad"

Notice the use of the global and case-insensitive switches. Without them newstr4 would be assigned "I'm sad. You're Happy".

replace() is actually a lot more useful than it would appear from this simple example. Recall from part two of our syntax discussion the use of /1, /2, etc to refer to the match for a parenthesized sub-expression. The RegExp object has similar properties called $1, $2. etc up to $9, equivalent to /1 and so on which can be fed back into replace(). For example


reg5 = /(I am)(\s\w*.\s)(You are)(\s\w*)/;
str5 = "I am working. You are asleep";
newstr5 = str5.replace(reg5, "$3$2$1$4");
                                    //newstr5 = "You are working. I am asleep"

As you can guess, $1 and $3 correspond to "I am" and "You are" respectively and are swapped accordingly by replace().

str.match()

Our last string method, match(), is very similar to replace() except that instead of returning a new string, it returns an array of matches to the global regular expression as a result.

In the case that the regular expression does not contain the global switch, the first element of the array will always return the match for the complete expression while subsequent elements will hold $1, $2, etc.


reg6 = /(I am)(\s\w*.\s)(You are)(\s\w*)/;
str6 = "I am working. You are asleep";
var newarr6 = str6.match(reg6);

So in this example, newarr[0] holds "I am working. You are asleep", newarr[1] holds "I am", newarr[2] holds " working. " and so on.

re.test()

The first of the methods attached to the RegExp object is very similar to the search() method we looked at earlier. It simply returns a boolean value, true or false, depending on whether or not a pattern can be matched to a sequence of characters in the given string. For example:

var string="96521234";
var reg= /965/;
var isin = reg.test(string);            // isin = true
 

If the pattern has the global flag set, it will set the lastIndex property of the RegExp object (see below) and continue the search from that point in the string when called again. If it does not have the flag set, lastIndex will be reset to 0.

re.exec()

The last of our RE-utilizing methods is exec(), which acts in a similar fashion to match() when the global switch is not used. It also has useful side-effects. Let's start with an example.

var string="965212234";
var reg= /(\d{2}2)/g;                   //Look for two digits followed by a 2
var results = reg.exec(string);         // = ["652", "652"]   Call 1
var results = reg.exec(string);         // = ["122", "122"]   Call 2

exec does a little more than you may first have guessed. In fact it populates all the static properties of the RegExp object, the reg object and updates details of the array too. exec() also behaves the same as test() with respect to the global flag being set. Should it not find a match, exec() returns null for the array.

Given the code above, the following tables demonstrate what each call to reg.exec(string) populates various properties with.

home / programming / javascript / professional / chap3 / 6
[next]

Copyright 1999 (1st Edition) and 2001 (2nd Edition) Wrox Press Ltd. and

JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Choosing the Right Online Backup Provider · Mother Avaya Nurtures Her Technology Partners · Software as a Service a Winning Model for Hotspot Provider


Created: February 12, 2001
Revised: February 26, 2001


URL: http://webreference.com/programming/javascript/professional/chap3/