Register to get access to free programming courses with interactive exercises

Modifiers Regular Expressions (Regexp)

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.

In the example below, the regular expression corresponds to a single substring:


/(?:t.)-(?:t.)/

ta-tu ta-t

Tu-tu tu-T


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.

The rest of the characters in the string does not match. Among them are capital T and a line break. We have not found:

  • The capital T because the character t in the expression is in lowercase
  • The line break because periods do not include a line break by default

We can modify the expression slightly to include the other substring from the example. To do this, we use a modifier.

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:


/(?i:t.)-(?:t.)/

ta-tu ta-t

Tu-tu tu-T


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:


/(?i:t.)-(?:t.)/

ta-tu ta-t

Tu-Tu tu-T


So let us duplicate the i modifier in the second group to 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].).

We can also place modifiers in separate groups:


/(?i)(t.)-(?i)(t.)

ta-tu ta-t

Tu-Tu tu-T


But in this case, we allocate our memory for four groups of matches.

Here we will look at another modifier — s. It makes it so that periods include line breaks and carriage returns. 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.

We will 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 - before 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


Also, we can combine active and disabled modifiers. We can add s and disable i and m:


/(?s-im:t.)-(?si:t.)/

ta-tu ta-t

Tu-Tu tu-T



Recommended materials

  1. Modifiers

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

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.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs
profession
Development of front-end components for web applications
10 months
from scratch
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

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.