Bummer! You have been redirected as the page you requested could not be found.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Regular Expressions in JavaScript!
You have completed Regular Expressions in JavaScript!
Preview
Flags let you change what a regular expression matches in some way. Learn about the most common flags.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Flags in regular expressions modify
the way the expression behaves.
0:00
There are a number of flags,
but we'll focus on three.
0:05
Each one of these flags overrides
a default behavior of regular expressions.
0:09
First, i stands for case-insensitive.
0:14
Using i will make the parser disregard
case when searching for matches.
0:18
G is for global.
0:23
By default, the regular expression will
stop searching once it finds a match.
0:24
The g flag tells the parser to find
all matches contained in a string.
0:29
The m flag stands for multiline.
0:34
Normally a caret will only match
the very beginning of a string and
0:36
a dollar sign will only match the end.
0:41
Any line breaks the string contains
will be considered part of the string.
0:44
Setting the m flag will cause the parser
to treat line breaks like new lines.
0:48
A caret will match the beginning
of each new line and
0:53
a dollar sign will match the end.
0:57
To add any of these flags
to a regex in JavaScript,
0:59
put them after the last
slash of a regex literal.
1:03
You can put them in any order.
1:07
Let's see these in use.
1:09
I'm still in my console,
I'll create a new string LION
1:11
in all caps and call replace on it.
1:16
I'll pass a regex matching lion in
lowercase and add the i flag to it.
1:20
And let's make
the replacement string mouse.
1:28
You can see LION was replaced.
1:33
Note, you could also use
a character set containing a to z.
1:35
All lowercase.
1:44
Let's use the global flag.
1:46
I'll use a slightly different string.
1:48
I'll clear my console and type,
1:50
she ate watermelon at the waterpark.
1:55
I'll look for water and
replace it with an empty string.
2:03
In other words,
I want to delete the string, water.
2:12
Notice how the parser found and
replaced the first occurrence of water and
2:15
left the second one alone.
2:19
If we wanted the parser to replace all
occurrences, we could add a g flag.
2:21
Now both occurrences of
the name have been replaced.
2:28
If there were more,
they would have been replaced too.
2:31
Now I'll create a variable called treat
and I'll store a multiline string in it.
2:34
Each line of the string
is the word cheese.
2:41
Now I'll call replace on it and
pass in the regular expression
2:49
matching a string that only
contains the word cheese.
2:54
I'll replace it with fruit.
3:04
The regex doesn't match it because
the string contains the word
3:08
cheese three times with new lines.
3:11
If I add the m flag,
3:15
you can see that the first instance
of cheese was changed to fruit.
3:19
The parser found the line break and
3:23
considered the first
instance of cheese to match.
3:25
The other instances were not matched
because we didn't set the global flag.
3:28
I'll put a g now.
3:33
All of the instances of
cheese are turned to fruit.
3:37
You'll see flags often in
other regular expressions and
3:40
it's good to know about them.
3:44
Later in the course for example, we'll
make use of the case-insensitive flag.
3:45
I'd probably use the global flag most of
all to replace occurrences of a name, for
3:50
example, or to delete blank lines.
3:55
You're ready to start adding validation
to the form I showed in an earlier video.
3:59
In the next video, I'll show you
the code we'll be starting with.
4:04
Then, we'll validate the user name input.
4:08
Let's get started.
4:11
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up