Register to get access to free programming courses with interactive exercises

Position within a string Regular Expressions (Regexp)

In this lesson, we'll learn about special characters that are used to clarify positions of characters in strings and substrings.

Let's look at the following example:



Here, the word java matches the string java. This is a simple condition. It's important to remember that regular expressions don't look for matches in strings, but in substrings. If you search in a string that contains characters besides the ones you're looking for, the check may show that the strings match, even though that isn't what you want:


asdfjava asdf

There are special characters to control character search in a string.

A character that represents the beginning of a line

If you put ^ in the search string before the characters you're looking for, only the characters that are at the beginning of the string will match:


java ruby clojurescript javascript

If we remove ^, we'll have two matches, including one in the last substring:


java ruby clojurescript javascript

A character that represents the end of a string

This character is $. Here is an example without using this character, with two matches:


java ruby clojurescript javascript

If we use $, then there's only one match, at the end of the string


java ruby clojurescript javascript

Searching for other characters relative to their word boundaries

Suppose we only need to find instances of a, that are at the end of a word. To do this, we type a\b in the string pattern:


java ruby clojurescript javascript

If we type \B, instead, we can invert the search and find all instances of a, that are not at the end of a word:


java ruby clojurescript javascript

If you put \b in front of the character we're searching for, the characters at the beginning of the word will be found:


java ruby clojurescript javascript

Using inverting again:


java ruby clojurescriptj javascript

For clarity, if we compare with the previous example, we added another j after the clojurescript in our string. Now let's find only instances of j that are neither at the beginning nor at the end of a word:


java ruby clojurescriptj javascript

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
hours of theory

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:">Bookmate</span>
<span class="translation_missing" title="translation missing:">Healthsamurai</span>
<span class="translation_missing" title="translation missing:">Dualboot</span>
<span class="translation_missing" title="translation missing:">Abbyy</span>
Suggested learning programs

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

Frontend Developer icon
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.