Boundaries are some ways to determinate the surroundings of our expressions and in which position they are inside our text. They are also known as anchors, and they do not consume characters.
The Beginning and End of a Line
Sometimes, specially working with multiline files, we want to certificate that our expression starts exactly at the beginning of a line, or fits properly at the end of a line, or even both. There are two metacharacters responsible for this.
The first one is the circumflex accent (^
), when we put it at the beginning of our regular expression we are basically saying that this is a beginning of a line.
Attention: In order to use this boundary, we need to use the m
flag. This flag treats our target as a multiline text, so it is possible to work with line anchors.
The other anchor is the dollar sign ($
). We put it at the end of our regular expression in order to guarantee that the line ends at that point. Check out the example below:
Regex:
/^Sometimes.+tears$/m
Matches:
The world passed by with lofty look
Sometimes his eyes were dashed with tears
Sometimes his lips with laughter shook.
In the example above, we created a regular expression that matches all lines that starts with the word Sometimes and ends with the word tears, with some characters between them. Pretty simple.
We can also use them separately, it is not necessary to have both of them in the same expression. Using the same poem of the previous example, we can create this:
Regex:
/^Sometimes.+/m
Matches:
The world passed by with lofty look
Sometimes his eyes were dashed with tears
Sometimes his lips with laughter shook.
This time, we only used the anchor at the beginning, so it matched only the lines that starts with the word Sometimes.
Word and Non-word Boundaries
The word boundary is useful when we what to specify that a expression is a word, but without consuming characters. They determinate if the characters surrounding our expression are word or non-word characters.
To use this boundary, we use the character shorthand \b
at the beginning or the end of our expression, or both. For a better understanding, there are three examples below:
Regex:
and
Matches:
A scented gift and remembrancer designedly dropped...
Regex:
\band\b
Matches:
A scented gift and remembrancer designedly dropped...
Regex:
\Band\B
Matches:
A scented gift and remembrancer designedly dropped...