JavaScript RegExp object

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

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

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

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";

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

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

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+");

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;

{
}

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

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;

OR

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

Рекомендации по теме
Комментарии
Автор

I don't understand what the m for multiline does, why you're using a while loop when the g in regexp already returns all the iterations, or what result[0] represents.

ralphedgar
Автор

Hi Venkat, will you be covering js objects, nested objects and pushing them into an array and sorting them, also passing object to other files like using define ?

badrulhussain
Автор

Nice work Try to release some java tutorials please

madhumadhu-jbmx
Автор

what is the use of javascript in site making and what is the use of php to make a site the and both language do the same thing or not

ImranKhan-uzrh
Автор

string manipulation and type casting is missing please update that too.

shashikumarabm
Автор

Nice tutorials. Just loved it..

What is the regexp for "../" ( without quotes) for global search ?

lipaksahu
Автор

/* une fonction qui rend ca possible?
1. isValid('[(yty)]') => true
2. isValid('[(]') => false
3. isValid('[a(fd)[}]') => false
4. isValid('[{43}(yty)[{65}]]') => true*/

julien