Path: blob/main/files/en-us/web/javascript/guide/regular_expressions/assertions/index.md
6517 views
------{{jsSidebar("JavaScript Guide")}}
Assertions include boundaries, which indicate the beginnings and endings of lines and words, and other patterns indicating in some way that a match is possible (including look-ahead, look-behind, and conditional expressions).
{{EmbedInteractiveExample("pages/js/regexp-assertions.html", "taller")}}
Types
Boundary-type assertions
| Characters | Meaning |
|---|---|
^ |
Matches the beginning of input. If the multiline flag is set to true,
also matches immediately after a line break character. For example,
Note: This character has a different meaning when it appears at the start of a character class. |
$ |
Matches the end of input. If the multiline flag is set to true, also
matches immediately before a line break character. For example,
|
\b |
Matches a word boundary. This is the position where a word character is not followed or preceded by another word-character, such as between a letter and a space. Note that a matched word boundary is not included in the match. In other words, the length of a matched word boundary is zero. Examples:
To match a backspace character ( |
\B |
Matches a non-word boundary. This is a position where the previous and
next character are of the same type: Either both must be words, or
both must be non-words, for example between two letters or between two
spaces. The beginning and end of a string are considered non-words.
Same as the matched word boundary, the matched non-word boundary is
also not included in the match. For example,
|
Other assertions
Note: The
?character may also be used as a quantifier.
| Characters | Meaning |
|---|---|
x(?=y) |
Lookahead assertion: Matches "x" only if "x" is
followed by "y". For example, |
x(?!y) |
Negative lookahead assertion: Matches "x" only if "x"
is not followed by "y". For example, |
(?<=y)x |
Lookbehind assertion: Matches "x" only if "x" is
preceded by "y". For example,
|
(?<!y)x |
Negative lookbehind assertion: Matches "x" only if
"x" is not preceded by "y". For example,
|
Examples
General boundary-type overview example
Matching the beginning of input using a ^ control character
Use ^ for matching at the beginning of input. In this example, we can get the fruits that start with 'A' by a /^A/ regex. For selecting appropriate fruits we can use the filter method with an arrow function.
In the second example ^ is used both for matching at the beginning of input and for creating negated or complemented character class when used within character classes.
Matching a word boundary
Lookahead assertion
Basic negative lookahead assertion
For example, /\d+(?!\.)/ matches a number only if it is not followed by a decimal point. /\d+(?!\.)/.exec('3.141') matches "141" but not "3.
Different meaning of '?!' combination usage in assertions and character classes
The ?! combination has different meanings in assertions like /x(?!y)/ and character classes like [^?!].