this is part 49 of JavaScript tutorial
in this video we'll discuss using
regular expressions with some of the
JavaScript string object methods here we
have a few of the JavaScript string
object methods with which we can use
regular expressions along with the
regular expressions we can also use
these modifiers to space for the kind of
search we want to perform to perform a
global search we use letter G and for
case insensitive search we use letter I
and for multi-line search letter M let's
look at a few examples of using regular
expressions along with these modifiers
let's first look at an example of using
match method here I have a string and
notice this string has got four numbers
within it now what we want to do is
retrieve all those numbers from the
string and then write them to the
webpage so I'm going to use document or
write method so this string is present
inside this variable string and string
object has got match method and if we
look at this method the first parameter
it takes is regular expression so we
want to retrieve all the numbers from
this string so we have to tell that to
the match method and we are going to
tell that using a regular expression
regular expressions must be inside
forward slashes and now we want to tell
the match method to give us the number
and to specify that we want in number we
are going to use this regular expression
backslash d plus so basically this
regular expression is telling that we
want to retrieve a number which has got
one or more digits in it with these
changes let's save it and load this page
notice that we only get the first number
but we want to retrieve all the numbers
from the given string that is we want to
do a global match and to specify that we
want to a global match we use this
modifier that is letter G so at the end
of the regular expression let's use
letter J save the changes reload the
page now notice that we get all the four
numbers back let's look at an example of
using replace method
let's say for example these numbers are
confidential and we don't want to
display them as he's on the webpage
since they are confidential maybe we
want to mask them with a different
string like xxx and to do that we have
Lee place method in JavaScript and if
you look at this replace method it has
got two parameters the first parameter
is the regular expression so we already
have the regular expression and the
second parameter is the replacement
value so we want to replace the numbers
with this train xxx
let's save these changes reload the page
notice that we get the original string
back but with the numbers masked with
the replacement value that we provided
to the replace method let's look at an
example of using split method what a
split method going to do split method
will split the string into an array of
sub strings but obviously we will have
to specify to the split method the
criteria based on which we want to split
the string so if you look at split
method now we have to specify the
separator okay now here we are using a
regular expression to specify the
separator so this specifies that
wherever you find a number split the
string okay so we are going to use just
the separator there and let's get rid of
the second parameter save the changes
reload the page now notice that wherever
it finds the number it's actually
splitting the string so that's the first
train that's the second string that is
the third string and that is the fourth
string so wherever it has found the
number it has split the string now
instead of using a regular expression I
can also use you know a string literal
like this so wherever you find a space
split the string so let's save the
changes now when we reload this page
notice that it splits wherever it finds
a single space character let's look at
an example of using search method what
is search method going to do search
method is actually going to return the
index of the first matching item if a
match is found if a match is not found
then it's going to return -1 let's say
we want to search again with search
method we can use a regular expression
backslash t + is going to specify a
number so search method here is going to
look for the first match
so the first match is found here so this
index position is 22 so let's save the
changes reload the page notice that we
get 22 back that is the index position
of the first matching item for example
let's say you know if a match is not
found then it is going to return -1
let's say I want to look for an item
number with 20 digits obviously we
cannot find that number in this string
so when we load this page notice that we
get minus 1 back now let's look at an
example of performing a global case
insensitive match let's actually modify
the string so here
tom has got both capital and small
letters and here let's say Tom is 35 all
small letters and here let's say tom is
in London all capital letters now let's
use the match method
now what we want to do is retrieve all
the occurrences of this word tom now if
I just use Tom like this so that's a
regular expression let's save the
changes load the page notice that it
only returns one occurrence Tom an exact
match from the given string now let's
say we want to retrieve all three
occurrences of Tom so that means we want
to perform a global case-insensitive
match now if I try to search for
something like this Tom with D capital m
capital and Oh small we don't have a Tom
with that combination in this string so
when we reload this page notice that we
don't get anything back we get a null
back okay now I actually want to perform
a global case insensitive match so for
that we can use modifiers G and I let's
save the changes reload the page notice
that we get all three occurrences of Tom
from that strain thank you for listening
and have a great day