Lookbehind in JavaScript regular expressions is quite new. As of this writing, it's only supported in V8 (in Chrome, Chromium, Brave...), not by other engines.
There are many questions with answers here about how to work around not having lookbehind, such as this one.
This article by Steven Levithan also shows ways to work around the absense of the feature.
I want to replace every 'i' that is NOT following/followed by any other i and replace it with 'z`
That's fairly easy to do without either lookahead or lookbehind, using placeholders and a capture group. You can capture what follows the i
:
const rex = /i(i+|.|$)/g;
...and then conditionally replace it if what was captured isn't an i
or series of i
s:
const result = input.replace(rex, (m, c) => {
return c[0] === "i" ? m : "z" + c;
});
Live Example:
const rex = /i(i+|.|$)/g;
function test(input, expect) {
const result = input.replace(rex, (m, c) => {
return c[0] === "i" ? m : "z" + c;
});
console.log(input, result, result === expect ? "Good" : "ERROR");
}
test("i", "z");
test("iki", "zkz");
test("iiki", "iikz");
test("ii", "ii");
test("iii", "iii");