filmov
tv
JavaScript RegExp object

Показать описание
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";
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";
Комментарии