In this lesson, we'll look at what modifiers are in regular expressions and how they can be used and what for. Modifiers are used differently in JavaScript than in the examples, so use PCRE to experiment with them.
In the example below, the regular expression corresponds to a single substring:
/(?:t.)-(?:t.)
/
ta-tu
ta-t
Tu-tu tu-T
Here, two groups of hyphenated ta-tu
characters are matched, each corresponding to this condition: "t
and any character". A grouping without backreferencing will only find an expression in its entirety, not two separate groups. The rest of the characters in the string aren't matched. Among them are capital T
and line break. Capital T
isn't found because the character t
in the expression is in lower case, and the second isn't found because periods by default don't include a line break.
We can modify the expression slightly so that it includes the other substring from the example. To do this, we'll use a modifier.
Modifiers are characters that are specified after ?
in a group of regular expression characters and that change their behavior. If we put i
after ?
, it makes it ignore case sensitivity, and we get get a match with another substring, but instead of lower case t
, we have an upper case T
:
/(?i:t.)-(?:t.)
/
ta-tu
ta-t
Tu-tu
tu-T
If we capitalize the second part of the substring after the hyphen in Tu-tu
, there won't be a match because the modifier only works within the group it's defined in.
/(?i:t.)-(?:t.)
/
ta-tu
ta-t
Tu-Tu tu-T
So let's duplicate the i
modifier in the second group and get a match for Tu-Tu
in the string:
/(?i:t.)-(?i:t.)
/
ta-tu
ta-t
Tu-Tu
tu-T
This entry is a shorter version of its counterpart: (?:[tT].)-(?:[tT].)
.
Modifiers can also be placed in separate groups:
/(?i)(t.)-(?i)(t.)
ta-tu
ta-t
Tu-Tu
tu-T
But in this case, memory will be allocated for 4 groups of matches.
Let's look at another interesting modifier s
. It makes it so that periods include line breaks and carriage returns. We already know that by default, periods don't include them, and we were able to check this at the beginning of the lesson. ta-t
and tu-T
are followed by a line break, so these substrings aren't matched in the string. Let's put the modifier s
in the second group; now all the substrings are matched:
/(?i:t.)-(?si:t.)
/
ta-tu ta-t
Tu-Tu tu-T
Modifiers can be disabled. All you have to do is put -
. in front of them. Let's add -
to the first group and look at our example:
/(?-i:t.)-(?si:t.)
/
ta-tu ta-t
Tu-Tu tu-T
Active and disabled modifiers can be combined. We can add s
and disable i
and m
:
/(?s-im:t.)-(?si:t.)
/
ta-tu ta-t
Tu-Tu tu-T
The Hexlet support team or other students will answer you.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
From a novice to a developer. Get a job or your money back!
Sign up or sign in
Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.