Good question. I've come up with an initial function which does something slightly different. It will extract the numerical value from a string and convert it to a number type.
Because numbers are formatted differently in various locales, you can define which characters are expected to be used as thousands separators (default=","), and which are used for the decimal point (default=".").
function extractNumber(value, thousands, decimals) {
thousands = thousands || ',';
decimals = decimals || '.';
var regexp = new RegExp("[+-]?\\d+(?:["+thousands+"]\\d{3})*(?:["+decimals+"]\\d+)?", "g");
value = value.match(regexp)[0];
value = value.replace(new RegExp('['+thousands+']', 'g'), '');
value = parseFloat(value);
return value;
};
Usage example:
extractNumber("hello-sdvf-1,234.23 23 everybody 4");
Returns
-1234.23 (a number, not a string)