Draft
This page is not complete.

Assertions indicate in some way that a match is possible. Assertions include look-ahead, look-behind, and conditional expressions.

Types

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, /Jack(?=Sprat)/ matches "Jack" only if it is followed by "Sprat".
/Jack(?=Sprat|Frost)/ matches "Jack" only if it is followed by "Sprat" or "Frost". However, neither "Sprat" nor "Frost" is part of the match results.

x(?!y)

Negative lookahead assertion: Matches x only if x is not followed by y. 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.

(?<=y)x

Lookbehind assertion: Matches x only if x is preceded by y. For example, /(?<=Jack)Sprat/ matches "Sprat" only if it is preceded by "Jack". /(?<=Jack|Tom)Sprat/ matches "Sprat" only if it is preceded by "Jack" or "Tom". However, neither "Jack" nor "Tom" is part of the match results.

(?<!y)x

Negative lookbehind assertion: Matches x only if x is not preceded by y. For example, /(?<!-)\d+/ matches a number only if it is not preceded by a minus sign. /(?<!-)\d+/.exec('3') matches "3". /(?<!-)\d+/.exec('-3')  match is not found because the number is preceded by the minus sign.

Examples

 

Browser support

 

See also

Document Tags and Contributors

Contributors to this page: Iluvat4r, pkay, chrisdavidmills, dima74, mdnwebdocs-bot, jpmedley
Last updated by: Iluvat4r,