Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

JavaScript RegExp object

Suggested Videos
Part 47 - Using regular expressions in JavaScript
Part 48 - Tools for writing regular expressions
Part 49 - JavaScript strings and regular expressions



In this video we will discuss RegExp object in JavaScript.



There are 2 ways to create a regular expression in JavaScript

Using a regular expression literal
var regex = /\d+/g;

All the examples so far in this video series used regular expression literal to create a regular expression. Regular expressions created using regular expression literals are automatically compiled when the script is loaded. So if you know that the regular expression is not going to change then use this approach for better performance.

The following example replaces all the numbers with XXX.

var string = "Tom contact number is 1011011010. His age is 35.";
string += "Mark contact number is 8912398912. His age is 45";

document.write(string.replace(/\d+/g, "XXX"));

Output : Tom contact number is XXX. His age is XXX.Mark contact number is XXX. His age is XXX 


Using the constructor function of the RegExp object

var regexp = new RegExp("\\d+", "g");

Regular expressions created using the constructor function are compiled at runtime. Use this approach when the regular expression is expected to change.

Please note : Since the first argument of the RegExp constructor is a string, you have to escape the backslash.

The following example replaces all the numbers with XXX. 

var string = "Tom contact number is 1011011010. His age is 35.";
string += "Mark contact number is 8912398912. His age is 45";

var regexp = new RegExp("\\d+", "g");

document.write(string.replace(regexp, "XXX"));

Output : Tom contact number is XXX. His age is XXX.Mark contact number is XXX. His age is XXX 

Commonly used RegExp object properties
javascript regexp object properties

Example : 
var regexp = new RegExp("\\d+", "gi");

document.write("g = " + regexp.global + "<br/>");
document.write("i = " + regexp.ignoreCase + "<br/>");
document.write("m = " + regexp.multiline + "<br/>");
document.write("source = " + regexp.source + "<br/>");

Output : 
JavaScript RegExp object

Commonly used RegExp object methods
javascript regexp object methods

exec() method returns the first match

var string = "Tom contact number is 1011011010. His age is 35.";
string += "Mark contact number is 8912398912. His age is 45";

var regexp = new RegExp("\\d+");
document.write(regexp.exec(string));

Output : 1011011010

To get all the matches call .exec() method repeatedly with the g flag as shown below

var string = "Tom contact number is 1011011010. His age is 35.";
string += "Mark contact number is 8912398912. His age is 45";

var regexp = new RegExp("\\d+", "g");
var result;

while ((result = regexp.exec(string)) != null)
{
    document.write(result[0] + "<br/>");
}

The following example calls test() method of the RegExp object to check if the string contains numbers.

var string = "Tom contact number is 1011011010. His age is 35.";
string += "Mark contact number is 8912398912. His age is 45";

var regexp = new RegExp("\\d+", "g");
document.write("String contain numbers - " + regexp.test(string))

Output : String contain numbers - true


You can also call exec() and test() methods of the RegExp object as shown below

var string = "Tom contact number is 1011011010. His age is 35.";
string += "Mark contact number is 8912398912. His age is 45";

var regexp = /\d+/g;
document.write("String contain numbers - " + regexp.test(string))

OR

var string = "Tom contact number is 1011011010. His age is 35.";
string += "Mark contact number is 8912398912. His age is 45";

document.write("String contain numbers - " + /\d+/g.test(string))

JavaScript tutorial

No comments:

Post a Comment

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.