WebWanderer WebWanderer - 6 months ago 22
Javascript Question

Regex difference between PHP and Javascript

I've found a bug in the lexer I wrote some time ago, and it appears to be in my regex. I use regex101 a lot, and I often forget to change from PHP to Javascript on the left. Either way, here is my regex:

/([\w\.]*)([()]*)/g


Now, aside from matching a whole lot of nothing, I should match the string:

(!one || !two) && three


...on:

(
,
one
,
two
,
)
,
three


In PHP, this regex works just the way I want it to!

In Javascript, it doesn't!!!! Why the heck does Javascript give me this?!?!

(
,
one
,
two)
,
three


Our parts
two
and
)
wound up in the same match...


So, my question is, how could I match the string:

(!one || !two) && three


...as:

(
,
one
,
two
,
)
,
three


..in Javascript using a
string.match()
on the regex?

Thanks!

Answer

So, my question is, how could I match the string:

(!one || !two) && three 

...as:

(, one, two, ), three

..in Javascript using regex?

As follows:

"(!one || !two) && three".match(/[\w\.]+|[()]/g);

["(", "one", "two", ")", "three"]

Why the heck does Javascript give me this?!?!

Your original regex /([\w\.]*)([()]*)/g matches 0 or more word characters or dots followed by 0 or more brackets (* means 0 or more). You're actually getting not just things like two) but also lots of empty strings. To get what you're asking for, you need to use alternation, as in the example above: you want either word chars or brackets.

I'm guessing you probably want at least one word char and only one bracket at a time, so I've amended the quantifiers to + on the word char option and nothing on the bracket option.