Q:

Regex comma as optional character

I have this regex that I made:
/\$([A-Za-z0-9\-\_ ]+)(\,?)/gi

I need to convert the following senteces:
$a, $b, $c, $hi = "", $na

Into this:
$a = NULL, $b = NULL, $c = NULL, $hi = "", $na = NULL

But I get this instead of the example above:
$a = NULL, $b = NULL, $c = NULL, $hi = "", $na

I'm using this to replace the values:
$$$1 = NULL$2

As you can see, if there isn't a comma, it won't work. 
Hope you guys can help me. 
Update: I just need to ignore it if it has "=" and a value:
For example, I have to convert this:
$a, $a, $a
$a, $a = '', $a
$a
$a,
$a = ''
$a = '',
$a = '', $a

Into this
$a = NULL, $a = NULL, $a = NULL
$a = NULL, $a = '', $a = NULL
$a = NULL
$a = NULL,
$a = ''
$a = '',
$a = '', $a = NULL

The '' can be also NULL, "" and integer values. 

A:

You may use
/\$([\w -]+)(?!\w|,?\s*=)(,?)/g

See the regex demo. 
Note that [A-Za-z0-9_] = \w, thus the regex pattern can be shortened as shown above. 
The (?! \w|,?\s*=) part is the main difference here: it makes sure there is no word char or an optional , followed with 0+ whitespaces and then = immediately after matching the Group 1 pattern, [\w -]+. 
See the JS demo:

const texts = ['$a, $a, $a', "$a, $a = '', $a", '$a', '$a,', "$a = ''", "$a = '',", "$a = '', $a", '$a, $b, $c, $hi = "", $na'];
const regex = /\$([\w -]+)(?!\w|,?\s*=)(,?)/g;
for (let s of texts) {
  console.log("`" + s + "` => `" + s.replace(regex, '$1 = NULL$2') + "`")
}

