Here is the video version for this topic. You can read the written version which comes after the video section.
In the previous lesson, we saw how to create a regular expression using literal notation.
In this lesson, we'll learn about flags--their relevance and how to use them.
What are Flags?
Flags are parameters you pass to a regex construct which alters some of its default behaviour.
There are a couple of flags, some of which may differ across different programming languages or regex engines. But in this article, we'll look at four common flags:
- g - Global Flag
- i - Case-Insensitive Flag
- s - Newline (or Single line) Flag
- m - Multiline Match Flag
In a literal notation, you can pass one or more flags after the last forward slash:
/pattern/flags
For example, you can pass the g and i flags like this:
/pattern/gi
But what do these flags mean? Let's look at them.
The Global Flag g
By default, your regex pattern will only return the first set of characters in a string that matches. If you want to match all set of characters that match your pattern, then you add this flag at the end of the regex.
In the previous lesson, I used this example:
This pattern matched only the first occurence of "code":
By passing a g flag to the regular expression, /code/g, the pattern will match more than one occurrence of "code":
The Case Insensitive Flag i
By default, your regex patterns are case sensitive. This means: /hello/ will not match the "Hello" in "Hello, how are you":
This is because the cases are not the same (lower "h" and capital "H" are different case-wise).
To remove the case restriction on your regex pattern, apply this flag. Now, /hello/i will match the "Hello" in your string:
The Newline Flag s
By default, the dot sign . is a special character that matches any kind of character except a newline.
Don't worry too much about special characters for now, there's a dedicated lesson for that. Let's just focus on the dot sign.
So, this flag allows the dot sign . to match newlines also.
For example, this pattern /hello.hi/ will match "hello hi", "hello_hi", "hello8hi":
But it will not match:
The reason is that . matches any character except a new line. But with the s flag, hello.hi/s, . can now match a newline:
The Multiline Match Flag m
The caret symbol, ^, when used at the beginning of a regular expression is used to match the beginning of a string, and the dollar symbol, $, when used at the end of a regular expression is used to match the end of a string.
Again, I have a separate lesson on special characters where we would get to see these symbols in more detail, but here is where the multiline match flag comes in.
Save we have this sentence:
Aisha likes to code.Aisha also likes to play basketball
And we have this pattern /Aisha/g. With the global flag g on, you see that it matches both "Aisha"s:
Using the caret symbol, called the beginning character, we can choose to match "Aisha" only if it begins the string. We do that by adding the caret symbol at the beginning:
Now you see that only the Aisha that begins the string is matched. But on the second line, "Aisha" begins the sentence right…why is it not matched?
Well, by default, again, the beginning character considers everything as one string, and only matches the beginning. Even if you have 10 lines, it would consider everything as one string, but if we want the beginning character to match different lines of the string, then we use the m flag:
You see now that "Aisha" that begins the first line is matched, and "Aisha" that begins the second line is matched.
But if I have something before "Aisha" on the second line, it is not matched anymore:
The same idea applies to the $ sign but for the end of a line. More about this in the special characters lesson.
Wrap up
There are many other flags out there for different regex engines…these 4 are more popular and widely supported.
You can see how these flags change the default behaviour in different ways. So when you're writing regular expressions, there are times you'd want to achieve something that is turned off by default…you'd need one or more of these flags.
As we progress in this course, you'd see us using g a lot.
I've been saying "regex engines" a lot (you probably noticed). What exactly are they? Let's look at that in the next lesson.