0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>In this lesson, we will learn about special characters that can clarify the character's positions in strings and substrings.</p>
1
<p>In this lesson, we will learn about special characters that can clarify the character's positions in strings and substrings.</p>
2
<p>Let us look at the following example:</p>
2
<p>Let us look at the following example:</p>
3
<p>/java/</p>
3
<p>/java/</p>
4
<p>java</p>
4
<p>java</p>
5
<p>Here, the word java matches the string java. It is a<strong>simple condition</strong>.</p>
5
<p>Here, the word java matches the string java. It is a<strong>simple condition</strong>.</p>
6
<p>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:</p>
6
<p>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:</p>
7
<p>/java/</p>
7
<p>/java/</p>
8
<p>asdfjava asdf</p>
8
<p>asdfjava asdf</p>
9
<p>There are special characters to control character search in a string.</p>
9
<p>There are special characters to control character search in a string.</p>
10
<h3>A character that represents the beginning of a line</h3>
10
<h3>A character that represents the beginning of a line</h3>
11
<p>If you put ^ in the search line before the characters you look for, only the characters at the beginning of strings will match:</p>
11
<p>If you put ^ in the search line before the characters you look for, only the characters at the beginning of strings will match:</p>
12
<p>/^java/</p>
12
<p>/^java/</p>
13
<p>java ruby clojurescript javascript</p>
13
<p>java ruby clojurescript javascript</p>
14
<p>If we remove ^, we will have two matches, including one in the last substring:</p>
14
<p>If we remove ^, we will have two matches, including one in the last substring:</p>
15
<p>/java/</p>
15
<p>/java/</p>
16
<p>java ruby clojurescript javascript</p>
16
<p>java ruby clojurescript javascript</p>
17
<h3>A character representing the end of a string</h3>
17
<h3>A character representing the end of a string</h3>
18
<p>This character is $. Here is an example without this character. It has two matches:</p>
18
<p>This character is $. Here is an example without this character. It has two matches:</p>
19
<p>/script/</p>
19
<p>/script/</p>
20
<p>java ruby clojurescript javascript</p>
20
<p>java ruby clojurescript javascript</p>
21
<p>If we use $, we will have only one match at the end of the string:</p>
21
<p>If we use $, we will have only one match at the end of the string:</p>
22
<p>/script$/</p>
22
<p>/script$/</p>
23
<p>java ruby clojurescript javascript</p>
23
<p>java ruby clojurescript javascript</p>
24
<h3>Searching for characters relative to their word boundaries</h3>
24
<h3>Searching for characters relative to their word boundaries</h3>
25
<p>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:</p>
25
<p>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:</p>
26
<p>/a\b/</p>
26
<p>/a\b/</p>
27
<p>java ruby clojurescript javascript</p>
27
<p>java ruby clojurescript javascript</p>
28
<p>If we type \B, instead, we can invert the search and find all instances of a not at the end of a word:</p>
28
<p>If we type \B, instead, we can invert the search and find all instances of a not at the end of a word:</p>
29
<p>/a\B/</p>
29
<p>/a\B/</p>
30
<p>java ruby clojurescript javascript</p>
30
<p>java ruby clojurescript javascript</p>
31
<p>If you put \b in front of the character we search for, we will find the characters at the beginning of the word:</p>
31
<p>If you put \b in front of the character we search for, we will find the characters at the beginning of the word:</p>
32
<p>/\bj/</p>
32
<p>/\bj/</p>
33
<p>java ruby clojurescript javascript</p>
33
<p>java ruby clojurescript javascript</p>
34
<p>Here we use inverting again:</p>
34
<p>Here we use inverting again:</p>
35
<p>/\Bj/</p>
35
<p>/\Bj/</p>
36
<p>java ruby clojurescriptj javascript</p>
36
<p>java ruby clojurescriptj javascript</p>
37
<p>For clarity, if we compare it with the previous example, we added another j after the clojurescript in our string.</p>
37
<p>For clarity, if we compare it with the previous example, we added another j after the clojurescript in our string.</p>
38
<p>Now we find only instances of j that are neither at the beginning nor the end of a word:</p>
38
<p>Now we find only instances of j that are neither at the beginning nor the end of a word:</p>
39
<p>/\Bj\B/</p>
39
<p>/\Bj\B/</p>
40
<p>java ruby clojurescriptj javascript</p>
40
<p>java ruby clojurescriptj javascript</p>
41
41