Below is a custom function that utilizes RegExp to identify and retrieve the first base 10 number from a given String
function findFirstNumber(inputString) {
return +inputString.replace(/^[\s\S]*?(-?(?:\d*\.)?\d+(?:e[+-]?\d+)?)[\s\S]*$/, '$1');
}
// positive
findFirstNumber('foo 123456789 bar'); // 123456789
// negative
findFirstNumber('foo -23456789 bar'); // -23456789
// decimal
findFirstNumber('foo 1234.6789 bar'); // 1234.6789
// e notation
findFirstNumber('foo 1234567e9 bar'); // 1234567000000000
findFirstNumber('foo 123456e+9 bar'); // 123456000000000
findFirstNumber('foo 123456e-9 bar'); // 0.000123456
findFirstNumber('foo 1234.6e-9 bar'); // 0.0000012346
// ignores e following number but not e notation
findFirstNumber('foo 12345678e bar'); // 12345678
// ignores subsequent decimal points
findFirstNumber('foo 1234.67.9 bar'); // 1234.678
// No numbers
findFirstNumber('foo bar'); // NaN
Therefore, in your specific scenario
var topMargin = findFirstNumber(elem.style.marginTop); // 30