I'm a beginner in regex.
I've been trying on regex101 trying to figure out a way to capture the variable identifiers in declarations. Basically the x in var x = 1; but not x = x + 1.
I'm working with PrismJS to try to highlight my code and it doesn't support non-capturing (?:) and it captures every partial matches but it has an option to ignore the first brackets to imitate a "look-behind" function.
I test with the following text:
var a = 1,
b=2,
c = 3;
var d = 5;
var x, y = 3,
z;
var x = 1,
z = 1.23E4, //comment
w = +Infinity,
u = NaN,
t = 0x6F, //hexidecimal
r = 0o37; //octadecimal
This far, I can only figure out a trick regex:
/(var *| *,\n* *)(\w+)(?= *=| *,| *;)/g
// not captured ^ not captured
// captured
The first brackets are ignored and not captured to imitate lookbehind. I can't make it work with var after | in the first brackets.
But of course, it will capture this as well which I don't want:
,
a =
Please help. There are so many limitations or maybe it's just me being too stupid, I can't construct a regex that works.