RegExp pattern - position (Definition)

Aligning the pattern to one or other end of the string sometimes helps to remove ambiguity from the match.


ECMAScript edition - 3

Occasionally, you may want to match a string that is explicitly at the front or back end of a line. You can do this by adding the special characters to the expression to mark the location of the start of the line or the end of the line. You can also align the pattern with word boundaries as well. Here is a summary of some special patterns that are position aligned:

^Indicate the start of the line
$Indicate the end of the line
\bIndicate a word boundary. Note that this cannot be used in a bracketed character class [\b] means backspace not word boundary
\BIndicate any non-word boundary location.
.$The last character at the end of the line (the dot matches one character)
\b\d*\bA complete word composed only of numeric digits
\b\w*\bA complete word
\s*$All of the trailing whitespace.
^$A line with nothing between the start and end, an empty line.
^.The first character at the beginning of the line (the dot matches one character)
^.*$The entire line regardless of its contents.
^\sA leading whitespace character.


See also:RegExp pattern, RegExp pattern - attributes, RegExp pattern - extension syntax, RegExp.multiline


ECMA 262 edition 3 - section - 15.10.1