Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Understanding The JavaScript RegExp Object
Find out all about the JavaScript RegExp object and its methods.

| Two To Tango |

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, which illustrates:


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

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
Trinity located in The Matrix at character 7
').'
'


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback