JS RegExp Assertions
In this page:
x(?=y) // lookahead
x(?!y) // negative lookahead
(?<=y)x // lookbehind
(?<!y)x // negative lookbehind
Positive Lookahead: (?=...)
A positive lookahead, written (?=pattern), requires the main match to be immediately followed by the given pattern, but does not include that following text as part of the actual match result -- useful for matching a word only when a specific word comes right after it.
उदाहरण: Positive Lookahead: (?=...)
console.log("100px".match(/\d+(?=px)/)); // matches digits only when followed by "px"
Negative Lookahead: (?!...)
A negative lookahead, (?!pattern), requires the main match to NOT be followed by the given pattern -- the opposite of a positive lookahead, useful for excluding a specific following context from an otherwise valid match.
उदाहरण: Negative Lookahead: (?!...)
console.log("100em".match(/\d+(?!px)/)); // matches since NOT followed by "px"
Positive and Negative Lookbehind: (?<=...) and (?<!...)
Lookbehind works like lookahead but checks the text immediately BEFORE the match instead of after -- (?<=pattern) requires that preceding text to be present, and (?<!pattern) requires it to be absent, again without including that preceding text in the actual match result.
उदाहरण: Positive and Negative Lookbehind: (?<=...) and (?<!...)
console.log("$100".match(/(?<=\$)\d+/)); // "100", lookbehind for $
console.log("100".match(/(?<!\$)\d+/)); // "100", not preceded by $
Combining Assertions with the Main Pattern
Assertions are typically combined with a real, capturing part of the pattern -- the assertion establishes context, while the surrounding pattern defines what actually gets matched and returned, together forming a precise condition beyond what plain character matching alone can express.
उदाहरण: Combining Assertions with the Main Pattern
console.log(/(?=.*\d).{6,}/.test("secret1")); // lookahead checks context, {6,} defines the real match
When Assertions Are the Right Tool
Assertions shine specifically when you need to match based on surrounding context without capturing that context -- extracting a price after a currency symbol, validating a password contains multiple different character types, or finding a word only when it is not part of a larger compound word.
उदाहरण: When Assertions Are the Right Tool
console.log("Price: $50".match(/(?<=\$)\d+/)); // extracts price after currency symbol without capturing the symbol itself
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS JSON
- JS Regular Expressions
- JS Fetch API
- JS LocalStorage and SessionStorage
- JS Cookies
- JS setTimeout and setInterval
- JS Event Loop
- JS Web Workers
- JS Service Workers
- JS AJAX
- JS AJAX Intro
- JS AJAX XMLHttp
- JS AJAX Request
- JS AJAX Response
- JS AJAX XML
- JS AJAX PHP
- JS AJAX Database
- JS JSONP
- JS RegExp Flags
- JS RegExp Classes
- JS RegExp Metachars
- JS RegExp Assertions
- JS RegExp Groups
- JS RegExp Quantifiers
- JS JSON HTML
- JS JSON vs XML