Register to get access to free programming courses with interactive exercises

Alternative Regular Expressions (Regexp)

Let's see how we can use regular expressions to find similar character combinations.

If we want to find the substrings gray or grow in the string, we need to use a mechanism called “alternation”: we enter the first possible option, followed by a “|”, followed by the second option. We can see the following match:


/gray|grow/

gray grow grey


The same condition can be written in an even shorter way. Since these substrings have a common part, let's enter gr characters and then, using grouping, add an alternative:


/gr(ay|ow)/

gray grow grey


Grouping is very important here. If we remove it, the alternative to gray becomes ow:


/gray|ow/

gray grow grey


Below is another interesting example of using alternation. While it's spelt gray in American English, in British English it's grey In order to avoid missing any of the options in the text, we can use a concise alternative:


/gr(a|e)y/

gray grow grey


And we can simplify the resulting expression, since the alternative uses single characters. Let's specify a character class that consists of the characters a and e:


/gr[ae]y/

gray grow grey


In this case, the regular expression mechanism, whose job is to match things, works much more efficiently with character classes, especially single classes.


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.