Use match:
a.match(/\$.*?\$/g);
This returns an array with all the values. You can also use
a.match(/\$.*?\$/g)||[];
to make sure that you’ve always got an array because if there’s no match you’ll get the null object which is not always useful.
The RegExp is also explained in an answer of mine to a similar question: match anything (.), any number of times (*), as few times as possible (?).
Then you can use join to join that Array into a String:
(a.match(/\$.*?\$/g)||[]).join(',');
Code:
var a='Dear $name$, This is my number $number$. This is my address $address$ Thank you!';
var b=(a.match(/\$.*?\$/g)||[]).join(',');
Output:
"$name$,$number$,$address$"
Effectively, in this case the regular expression matches every $ followed by anything up to the next $ and finally that dollar sign at the end. And match will give a list of results if you specify the g (global) flag at the end.
As this is a string (and the above a regular expression) literal, there’s no interference with jQuery’s $ symbol. The only important thing is to escape that symbol with a backslash (\$) because it has a special meaning in RegExp.