- A character that represents the beginning of a line
- A character representing the end of a string
- Searching for characters relative to their word boundaries
In this lesson, we will learn about special characters that can clarify the character's positions in strings and substrings.
Let us look at the following example:
/java
/
java
Here, the word java
matches the string java
. It is a simple condition.
It is important to remember that regular expressions do not look for matches in strings but in substrings. If you search in a string containing characters besides the ones you look for, the check may show that the strings match, even though you did not want it:
/java
/
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 line before the characters you look for, only the characters at the beginning of strings will match:
/^java
/
java
ruby clojurescript javascript
If we remove ^
, we will have two matches, including one in the last substring:
/java
/
java
ruby clojurescript java
script
A character representing the end of a string
This character is $
. Here is an example without this character. It has two matches:
/script
/
java ruby clojurescript
javascript
If we use $
, we will have only one match at the end of the string:
/script$
/
java ruby clojurescript javascript
Searching for characters relative to their word boundaries
Suppose we only need to find instances of a
at the end of a word. To do this, we type a\b
in the string pattern:
/a\b
/
java
ruby clojurescript javascript
If we type \B
, instead, we can invert the search and find all instances of a
not at the end of a word:
/a\B
/
ja
va ruby clojurescript ja
va
script
If you put \b
in front of the character we search for, we will find the characters at the beginning of the word:
/\bj
/
j
ava ruby clojurescript j
avascript
Here we use inverting again:
/\Bj
/
java ruby cloj
urescriptj
javascript
For clarity, if we compare it with the previous example, we added another j
after the clojurescript
in our string.
Now we find only instances of j
that are neither at the beginning nor the end of a word:
/\Bj\B
/
java ruby cloj
urescriptj javascript
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
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.