Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JavaScript RegEx working on browser however not working on PHPDesktop v5.7 Windows

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 <.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading