How about:
// returns array: ["0px", "3px", "2px"]
"margin: 0px 3px 2px;".match(/\d+(?:px|em)?/g)
Use g (global) flag, to match against whole string instead of stopping at first match.
// other example - returns array: ["3em", "2px", "39"]
"margin: 3em 2px 39;".match(/\d+(?:px|em)?/g)
To further clarify how this works:
\d+ matches numbers (one or more following each other)
(?:px|em)? skips capturing (but still matches!) this group - looks for px or em strings. Question mark at the end means that this whole group may not be found (for when there are numbers without units).
Edit: To answer OP concerns from the comment below.
Doing this: new RegExp(/\d+(?:px|em)?/g).exex(mystring) doesn't work because of two reasons.
First - you can't provide flags (g) when creating new RegExp from another (which is the case here - /\d+(?:px|em)?/g is equivalent of RegExp object, just like [] is equivalent of new Array(). Correct way of creating RegExp object using new RegExp construct is passing two string arguments. First is pattern, second are flags. So it above would become: new RegExp('\d+(?:px|em)?', 'g').
Second thing is that .exec() method always returns first match (not all) so it won't work for what you're after.
So, to reuse that regular expression in couple of places in your script you would have to assign it to variable, and then pass to .match() function. It could look like this:
var reg = new RegExp('\d+(?:px|em)?', 'g');
// somewhere down the road...
mystring.match(reg);
// and on some other string...
myotherstring.match(reg);
:, then split by space character. You should get an array containing the 0px, 3px, 2px tokens. The "solution" above may or may not work for all cases, since I don't know the exact format of CSS rule, so you need to look at the specification.