JavaScript RegExp object04:33

  • 0
Published on February 5, 2018

Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

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

global – returns true if the global modifier (g) is set, otherwise false
ignoreCase – returns true if the case-insensitive modifier (i) is set, otherwise false
multiline – returns true if the multi-line modifier (m) is set, otherwise false
source – Returns the text of the regular expression

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/]”);

Commonly used RegExp object methods

exec() – Tests for a match in a given string and returns the first match if found otherwise null.
test() – Tests for a match in a gievn string and returns true or false
toString() – Returns the string value of the regular expression

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

Enjoyed this video?
"No Thanks. Please Close This Box!"