User Id :    Password :      New Member   Forgot Password  
 
Defining RegExp in JavaScript
Description The RegExp object is used to store the search pattern.   No. of Views     1366
  Rating     4
Author Najmul Hoda   Posted On     11 Feb 2010
Tags Java Script    

Sample Code   Download Code

Introduction
The RegExp object is used to store the search pattern.
We define a RegExp object with the new keyword. The following code line defines a RegExp object called patt1 with the pattern "e": <pre name="code" class="brush:c-sharp"> var patt1=new RegExp("e"); </pre> When you use this RegExp object to search in a string, you will find the letter "e".


Methods of the RegExp Object
The RegExp Object has 3 methods: test(), exec(), and compile().

test() method
The test() method searches a string for a specified value. Returns true or false

Example

<pre name="code" class="brush:c-sharp"> var patt1=new RegExp("e") document.write(patt1.test("The best things in life are free")); Since there is an "e" in the string, the output of the code above will be:
true

exec() Method
The exec() method searches a string for a specified value. Returns the text of the found value. If no match is found, it returns null

Example 1

<pre name="code" class="brush:c-sharp">var patt1=new RegExp("e"); document.write(patt1.exec("The best things in life are free")); Since there is an "e" in the string, the output of the code above will be:
e
You can add a second parameter to the RegExp object, to specify your search. For example; if you want to find all occurrences of a character, you can use the "g" parameter ("global").

When using the "g" parameter, the exec() method works like this:
•    Finds the first occurence of "e", and stores its position
•    If you run exec() again, it starts at the stored position, and finds the next occurence of "e", and stores its position
Example 2
<pre name="code" class="brush:c-sharp"> var patt1=new RegExp("e","g"); do { result=patt1.exec("The best things in life are free"); document.write(result); } while (result!=null) Since there is six "e" letters in the string, the output of the code above will be:

eeeeeenull

compile() Method
The compile() method is used to change the RegExp.
compile() can change both the search pattern, and add or remove the second parameter.
Example
<pre name="code" class="brush:c-sharp"> var patt1=new RegExp("e"); document.write(patt1.test("The best things in life are free")); patt1.compile("d"); document.write(patt1.test("The best things in life are free")); Since there is an "e" in the string, but not a "d", the output of the code above will be:
truefalse

About Author

About Author He is a team lead, having 8+ years of experience. He loves writing technical article for .Net developers. Najmul Hoda
No Photo
 
Country United States
Company BirlaSoft
Home Page http://www.magixchat.com

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

 
 
Posted By Nitesh Sharma on 01 Mar 2010 at 10:09 PM
 
good job.... keep it up
 
 
 
Posted By Suman Trivedi on 25 Feb 2010 at 12:50 AM
 
Nice job..........
But it should be more explainable.......

My vote is 3
 
 
   
Write your comment here.
Comment
Verification Code