JavaScript strings and regular expressions

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 using regular expressions with some of the JavaScript string object methods.

In JavaScript regular expressions can be used with the following string methods.
1. match()
2. replace()
3. split()
4. search()

Along with regular expressions you can use modifiers to specify the kind of search you want to perform.
g Global search
i Case-insensitive search
m Multiline search

Let us look at some examples of using the above methods with regular expressions

Using regular expression with JavaScript string object's match() method. All the numbers in the string will be returned. The letter g at the end of the regular expression performs a global match. If the letter g is omitted we will only get the first match.

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

Output : 1011011010,35,8912398912,45

Using regular expression with JavaScript string object's replace() method. All the numbers in the string will be replaced 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 regular expression with JavaScript string object's split() method. split() method breaks a string into an array of substrings. The following example breaks the string into an array of substrings wherever it finds a number.

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 ,. His age is ,.Mark contact number is ,. His age is ,

Using regular expression with JavaScript string object's search() method. search() method returns the index of the match, or -1 if the search item is not found. search() method returns the index of the first matching item only. The following example returns the index of the first occurrence of a number in the string.

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

Global case insensitive match using a regular expression

var string = "TOM contact number is 1011011010. tom is 35";

Output : TOM,tom
Рекомендации по теме
Комментарии
Автор

Thank you for creating this, a very practical and concise explanation.... loved it!

sarthakdixit
Автор

hi kudvenkat

I'm big fan of your great videos.

Are you considering of uploading Cloud related videos(such as, how to create Azure platform, how to deploy our web site or any other projects, how to manage the platform... etc.. since everybody is moving to cloud now)? That would be really cool.

I already recommended your videos/uploads to my friends in UK and Turkey. we are all your fan.

many many thanks for your hard work so far. 

elifayse
Автор

very great video, really made me understand regex

shammahagwor
Автор

while i am doing this in my id am getting null ..

shivaramkrishna
Автор

These Videos are not playing at all
JavaScript arguments object
JavaScript Cookies
JavaScript strings and regular expressions
JavaScript substring example
Set and get multiple cookies in JavaScript

XTimNet