Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex works on browser but not in Node.js

I have a regex: /(?<!\<)\<[a-zA-Z0-9. _]+\>/g. Works fine in browser console if I do "(<<a1>> * <b1> * <c1> * <d1>) * <<e1>>".match(/(?<!\<)\<[a-zA-Z0-9. _]+\>/g).

Same line of code throws SyntaxError: Invalid regular expression: /(?<!<)<([a-zA-Z0-9. _]+)>/: Invalid group when executed in Node.js.

I want to match all variables in my string enclosed in < and > ignore them if enclosed by << and >>.

like image 264
Abhijit Borade Avatar asked Oct 12 '25 08:10

Abhijit Borade


1 Answers

As an answer as well (more space):
Unless node.js uses it's own regex engine and not the JavaScript one, lookbehinds are not supported in JS, thus (?<!) cannot work. To somewhat mimic this feature from other programming languages, have a look at Flagrant Badassery or use additional packages like node-re2 or node-perl-regex.

As for the differences between Browsers, Chrome does support lookbehinds.

like image 192
Jan Avatar answered Oct 14 '25 00:10

Jan