HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>In this lesson, we will look at modifiers in regular expressions. Modifiers are used differently in JavaScript than in the examples, so use PCRE to experiment with them.</p>
1 <p>In this lesson, we will look at modifiers in regular expressions. Modifiers are used differently in JavaScript than in the examples, so use PCRE to experiment with them.</p>
2 <p>In the example below, the regular expression corresponds to a single substring:</p>
2 <p>In the example below, the regular expression corresponds to a single substring:</p>
3 <p>/(?:t.)-(?:t.)/</p>
3 <p>/(?:t.)-(?:t.)/</p>
4 <p>ta-tu ta-t</p>
4 <p>ta-tu ta-t</p>
5 <p>Tu-tu tu-T</p>
5 <p>Tu-tu tu-T</p>
6 <p>Here, we have matched two groups of hyphenated ta-tu, each corresponding to this condition -t and any character. A grouping without backreferencing will only find a whole expression but not two separate groups.</p>
6 <p>Here, we have matched two groups of hyphenated ta-tu, each corresponding to this condition -t and any character. A grouping without backreferencing will only find a whole expression but not two separate groups.</p>
7 <p>The rest of the characters in the string does not match. Among them are capital T and a line break. We have not found:</p>
7 <p>The rest of the characters in the string does not match. Among them are capital T and a line break. We have not found:</p>
8 <ul><li>The capital T because the character t in the expression is in lowercase</li>
8 <ul><li>The capital T because the character t in the expression is in lowercase</li>
9 <li>The line break because periods do not include a line break by default</li>
9 <li>The line break because periods do not include a line break by default</li>
10 </ul><p>We can modify the expression slightly to include the other substring from the example. To do this, we use a<strong>modifier</strong>.</p>
10 </ul><p>We can modify the expression slightly to include the other substring from the example. To do this, we use a<strong>modifier</strong>.</p>
11 <p>Modifiers are characters specified after ? in a group of regular expression characters to change their behavior. If we put i after ?, it will ignore case sensitivity, and we get a match with another substring. But instead of lower case t we have an upper case T:</p>
11 <p>Modifiers are characters specified after ? in a group of regular expression characters to change their behavior. If we put i after ?, it will ignore case sensitivity, and we get a match with another substring. But instead of lower case t we have an upper case T:</p>
12 <p>/(?i:t.)-(?:t.)/</p>
12 <p>/(?i:t.)-(?:t.)/</p>
13 <p>ta-tu ta-t</p>
13 <p>ta-tu ta-t</p>
14 <p>Tu-tu tu-T</p>
14 <p>Tu-tu tu-T</p>
15 <p>Imagine we capitalize the second part of the substring after the hyphen in Tu-tu. There would not be a match because the modifier only works within the group where we defined it:</p>
15 <p>Imagine we capitalize the second part of the substring after the hyphen in Tu-tu. There would not be a match because the modifier only works within the group where we defined it:</p>
16 <p>/(?i:t.)-(?:t.)/</p>
16 <p>/(?i:t.)-(?:t.)/</p>
17 <p>ta-tu ta-t</p>
17 <p>ta-tu ta-t</p>
18 <p>Tu-Tu tu-T</p>
18 <p>Tu-Tu tu-T</p>
19 <p>So let us duplicate the i modifier in the second group to get a match for Tu-Tu in the string:</p>
19 <p>So let us duplicate the i modifier in the second group to get a match for Tu-Tu in the string:</p>
20 <p>/(?i:t.)-(?i:t.)/</p>
20 <p>/(?i:t.)-(?i:t.)/</p>
21 <p>ta-tu ta-t</p>
21 <p>ta-tu ta-t</p>
22 <p>Tu-Tu tu-T</p>
22 <p>Tu-Tu tu-T</p>
23 <p>This entry is a shorter version of its counterpart: (?:[tT].)-(?:[tT].).</p>
23 <p>This entry is a shorter version of its counterpart: (?:[tT].)-(?:[tT].).</p>
24 <p>We can also place modifiers in separate groups:</p>
24 <p>We can also place modifiers in separate groups:</p>
25 <p>/(?i)(t.)-(?i)(t.)</p>
25 <p>/(?i)(t.)-(?i)(t.)</p>
26 <p>ta-tu ta-t</p>
26 <p>ta-tu ta-t</p>
27 <p>Tu-Tu tu-T</p>
27 <p>Tu-Tu tu-T</p>
28 <p>But in this case, we allocate our memory for four groups of matches.</p>
28 <p>But in this case, we allocate our memory for four groups of matches.</p>
29 <p>Here we will look at another modifier - s. It makes it so that periods include line breaks and<a>carriage returns</a>. We already know that periods do not include them by default. The substrings ta-t and tu-T have line breaks, so we do not process them.</p>
29 <p>Here we will look at another modifier - s. It makes it so that periods include line breaks and<a>carriage returns</a>. We already know that periods do not include them by default. The substrings ta-t and tu-T have line breaks, so we do not process them.</p>
30 <p>We will put the modifier s in the second group; now all the substrings are matched:</p>
30 <p>We will put the modifier s in the second group; now all the substrings are matched:</p>
31 <p>/(?i:t.)-(?si:t.)/</p>
31 <p>/(?i:t.)-(?si:t.)/</p>
32 <p>ta-tu ta-t</p>
32 <p>ta-tu ta-t</p>
33 <p>Tu-Tu tu-T</p>
33 <p>Tu-Tu tu-T</p>
34 <p>Modifiers can be disabled. All you have to do is put - before them. Let's add - to the first group and look at our example:</p>
34 <p>Modifiers can be disabled. All you have to do is put - before them. Let's add - to the first group and look at our example:</p>
35 <p>/(?-i:t.)-(?si:t.)/</p>
35 <p>/(?-i:t.)-(?si:t.)/</p>
36 <p>ta-tu ta-t</p>
36 <p>ta-tu ta-t</p>
37 <p>Tu-Tu tu-T</p>
37 <p>Tu-Tu tu-T</p>
38 <p>Also, we can combine active and disabled modifiers. We can add s and disable i and m:</p>
38 <p>Also, we can combine active and disabled modifiers. We can add s and disable i and m:</p>
39 <p>/(?s-im:t.)-(?si:t.)/</p>
39 <p>/(?s-im:t.)-(?si:t.)/</p>
40 <p>ta-tu ta-t</p>
40 <p>ta-tu ta-t</p>
41 <p>Tu-Tu tu-T</p>
41 <p>Tu-Tu tu-T</p>
42  
42