The following code works perfectly on the browser:
console.log("abc> >".replace(/>(?<!abc>)/gm, "<"));
However, when I run it on PHPDesktop V5.7 Windows it comes up with the following error:
Uncaught SyntaxError: Invalid regular expression: />(?<!abc>)/: Invalid group
This could be due to the older version of JavaScript I believe is running on this version of PHPDesktop, however, I wonder why the RegEx would be different.
What I am trying to do in the RegEx is to replace the character ‘>’ with ‘>’ only if it is not preceded by ‘abc’ hence:
abc> >
would become:
abc> <
Is there another way I can do this?
>Solution :
The issue is that PHPDesktop uses an earlier version of JavaScript which does not support lookbehinds. Here is one workaround using a regex replacement with a callback:
var input = "abc> >";
var output = input.replace(/\babc>|>/g, x => x === "abc>" ? "abc>" : "<");
console.log(input);
console.log(output);
The idea behind the regex pattern \babc>|> is to first try to match abc>, followed by > by itself. Then, in the callback logic, we no-op for abc> and return the same thing, otherwise we replace > with <.