mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-25 21:16:21 +00:00
602 B
602 B
date | id | title |
---|---|---|
20201109 | d1549dd2-a5fc-4583-a186-5de02ca40d33 | JavaScript Lookbehind Assertions |
Examples
Positive lookbehind assertion
Text preceding the current location must match the assertion
const RE_DOLLAR_PREFIX = /(?<=\$)foo/g;
console.log("$foo %foo foo".replace(RE_DOLLAR_PREFIX, "bar")); // '$bar %foo foo'
Negative lookbehind assertion
Text preceding the current location must not match the assertion
const RE_NO_DOLLAR_PREFIX = /(?<!\$)foo/g;
console.log("$foo %foo foo".replace(RE_NO_DOLLAR_PREFIX, "bar")); // '$foo %bar bar'