'^' and the dollar
'$' symbols have special meaning in a regexp. They are called anchors.
'^' matches at text start and the dollar
'$' matches at text end. Both of them match the position in the text, not a character.
The example below matches the position at text start, and then
showMatch( 'Dolly doll', /^doll/gi ) // "Doll" (at text start)
'$' symbol matches at the text end.
The example below matches
doll and then there must be text ending position:
showMatch( 'Dolly doll', /doll$/gi ) // "doll" (at text end)
Anchors can be used together. For example,
^doll$ will match only if the text is exactly
When the regexp engine meets
'$', it only checks if the position is right, and does not consume any characters.
Suggest a string to match
We need a string which which starts and immidiately ends.
Or, being closer to regexp mechanics, the engine checks for text start position, and immediately checks for text end position without matching any characters.
It is possible only for the empty string.
The multiline mode is enabled by the flag
In multiline mode, anchors match line beginning (
^) and line end (
$) besides the start and end of the whole text.
In the example below,
/^\d+/gm matches a line start followed by a number:
showMatch( '*!*1*/!*st: John\n' + '*!*2*/!*nd: Mary\n' + '*!*33*/!*rd: Peter\n', /^\d+/gm ) // 1, 2, 33
In the following example, the caret
'^' is used in the middle of the pattern
/\w+\n^\d+/gm. Also works:
showMatch( '1st: *!*John\n*/!*' + '*!*2*/!*nd: *!*Mary\n*/!*' + '*!*33*/!*rd: Peter', /\w+\n^\d+/gm ) // "John\n2", "Mary\n33"
The end-of-line anchor
'$' behaves in a similar way. The following example matches words at the end of line:
showMatch( '1st: *!*John*!*\n' + '2nd: *!*Mary*/!*\n' + '33rd: *!*Peter*/!*', /\w+$/gm ) // John, Mary, Peter
Please note that
$ as well as
^ doesn’t add
\n to the match. They only check that the position is right.
- The caret
'^'matches the position at the the text start. In multiline mode it also matches after the newline symbol.
- The dollar
'$'matches the position at the text end. In multiline mode it also matches before the newline symbol.
For both anchors, the regexp engine only checks the position, and doesn’t match a character.