Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript regex (negative) lookbehind not working in firefox

I need to modify the following javascript regex because the negative lookbehind in it throws an error in firefox:

content = content.replace(/(?![^<]*>)(?:[\"])([^"]*?)(?<!=)(?:[\"])(?!>)/g, 'β€ž$1β€œ');

Does anyone have an idea and can help me out?

like image 672
Cla Avatar asked Apr 24 '18 22:04

Cla


People also ask

Can I use negative Lookbehind?

The positive lookbehind ( (? <= ) ) and negative lookbehind ( (? <! ) ) zero-width assertions in JavaScript regular expressions can be used to ensure a pattern is preceded by another pattern.

What is negative Lookbehind regex?

In negative lookbehind the regex engine first finds a match for an item after that it traces back and tries to match a given item which is just before the main match. In case of a successful traceback match the match is a failure, otherwise it is a success.

What is Lookbehind regex?

Lookbehind has the same effect, but works backwards. It tells the regex engine to temporarily step backwards in the string, to check if the text inside the lookbehind can be matched there. (? <!a)b matches a β€œb” that is not preceded by an β€œa”, using negative lookbehind.

Does Safari support negative Lookbehind?

Please Note: Lookbehind is not supported in non-V8 browsers, such as Safari, Internet Explorer.


2 Answers

July 1, 2020 Update

Starting with the FireFox 78 version, RegExp finally supports lookbehinds, dotAll s flag, Unicode escape sequences and named captures, see the Release Notes:

New RegExp engine in SpiderMonkey, adding support for the dotAll flag, Unicode escape sequences, lookbehind references, and named captures.

Thank you very much, FireFox developers!!! πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘


Lookbehinds are only available in browsers supporting ECMA2018 standard, and that means, only the latest versions of Chrome can handle them.

To support the majority of browsers, convert your pattern to only use lookaheads.

The (?<!=) negative lookbehind makes sure there is no = immediately to the left of the current location. [^"] is the atom that matches that character (note that ? quantifier makes it optional, but " that is before [^"] can't be = and there is no need restricting that position).

So, you may use

content = content.replace(/(?![^<]>)"([^"=]?)"(?!>)/g, 'β€ž$1"');
                                      ^^^^^

Note that (?:[\"]) is equal to ". [^"=]? matches 1 or 0 occurrences of a char other than " and =.

See the regex demo.

like image 179
Wiktor StribiΕΌew Avatar answered Oct 06 '22 13:10

Wiktor StribiΕΌew


Lookbehind assertions are part of ES2018. They are not yet supported by firefox, that's why you're getting an error.

Chrome supports them since version 62, and you can use them in Node.js >= 6.4 with the harmony flag, or in >= 9 without any flag.

You can check the proposal here & browser support here

like image 44
Marcos Casagrande Avatar answered Oct 06 '22 13:10

Marcos Casagrande