alt
alt

Reputation: 13907

RegExp match until end of word

So I'm trying to write a JavaScript variable-highlighting regular expression, so that variable declaration names are highlighted by the following code:

var foo = 'bar',
    raz = 'zar',
    wat

So far I have a regexp that whichights foo and raz but not wat since the regexp works by ending with the = character.

(var|,)(.*?)=

How can I modify this regexp so that is also highlights wat?

I have a feeling that the missing piece is to swap the = with an ending word boundary match.

Upvotes: 2

Views: 7706

Answers (2)

anubhava
anubhava

Reputation: 785156

Can you try this regex:

(?:var|,)\s*(\w+)(?= *=|$)

Live Demo: http://rubular.com/r/WEfFpAtWJ2

Upvotes: 1

Zeus
Zeus

Reputation: 6566

You can try this

(var)((.*)=(.*),?)+

Upvotes: 1

Related Questions