Defining Regular Expression in JavaScript

No.of Views1535
Bookmarked0 times
Downloads 
Votes1
By  ansari.najmul@gmail.com   On  13 Aug 2010 23:08:04
Tag : JavaScript , Utilities
In this I have explain the use of RegExp object in JavaScript. There are 3 key methods to use in real world scenarios. You can find examples in my article.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

The Regular Expression object is used to store the search pattern.We define a RegExp object with the new keyword.

Implementation

The following code line defines a RegExp object called patt1 with the pattern "e":

var patt1=new RegExp("e");

 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,

  1. test()
  2. exec()
  3. compile()

 test() method

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

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

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

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

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

Conclusion

In this I have explain the use of RegExp object in JavaScript. There are 3 key methods to use in real world scenarios. You can find examples in my article.

 
Sign Up to vote for this article
 
About Author
 
ansari.najmul@gmail.com
Occupation-Software Engineer
Company-NetEdge Computing Solutions
Member Type-Fresh
Location-India
Joined date-12 Aug 2009
Home Page-dotnetlogix.com
Blog Page-
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top