I'm trying to write a regular expression in JavaScript to search a multi-line bash script and extract all the variable names. I can neither seem to figure it out myself nor find a script that does exactly what I need.
I've gotten this far:
var re = /(?:^|\W)\$(\w+)(?!\w)/g;
var s = "test $test1 testagain$test2 testyetagain $test3 $test4$test5";
var matches = [];
while (match = re.exec(s)) {
matches.push(match[1]);
}
This will give me "test1", "test3", and "test4". But I would also like to get "test2" and "test5".
It would also be great if there were a way I could get "\$" to NOT match if it appears in the string. In other words, is there a way to escape the "$" string in the text so that it is ignored by my regular expression? So in case "\$1.00" appeared in the text, it would not match "1" as the above regex does now.
Thanks in advance for any help or for pointing me in the right direction.
PS This is actually for Action Script 3 but anything that works in JavaScript pre-ES6 should work in AS 3.
PPS The final goal is to replace these matches with variables from a key-value hash.
