Register to get access to free programming courses with interactive exercises

Modifiers Regular Expressions (Regexp)

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



Recommended materials

  1. Modifiers

Hexlet Experts

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

From a novice to a developer. Get a job or your money back!

Frontend Developer icon
Profession
beginner
Development of front-end components for web applications
start anytime 10 months

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.