wiki/content/20201109132457-javascript_lookahead_assertions.md

615 B

date id title
2020-11-09 3ab00edf-6c63-4db2-b32e-d18f8d683329 JavaScript Lookahead Assertions

Examples

Positive lookahead assertion

Text after the current location must match the assertion

const RE_AS_BS = /aa(?=bb)/;
const match1 = RE_AS_BS.exec("aabb");
console.log(match1[0]); // 'aa'

Negative lookahead assertion

Text after the current location must not match the assertion

const RE_AS_NO_BS = /aa(?!bb)/;

console.log(RE_AS_NO_BS.test("aabb")); // false
console.log(RE_AS_NO_BS.test("aab")); // true
console.log(RE_AS_NO_BS.test("aac")); // true