JSFiddle - React, Tailwind, and code Playground
HTML
<table>
<thead>
<tr>
<th>Value</th>
<th>ParseFloat</th>
<th>Regex</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
CSS
table { border-collapse: collapse; }
th, td { padding: .25em .5em; border: 1px solid #AAA; }
JavaScript
function unformatParseFloat(input) {
return parseFloat(input);
}
function unformatRegex(str){
// Matt's solution
var decimals = str.match(/([^0-9]{1})([0-9]{2,3})$/);
var num = str.match(new RegExp('^[^' + decimals[1] + ']+'));
num = num[0].replace(/[^0-9]+/g, '');
return parseFloat(num + '.' + decimals[2], 10);
}
function makeRow(input) {
var row = "<tr><td>" + input + "</td>\n";
functionArray.forEach(function(fn){
row += '<td>' + fn(input) + '</td>\n';
});
row += "</tr>\n";
$body.append(row);
}
function processValues() {
values.us.forEach(function(val){ makeRow(val); });
values.other.forEach(function(val){ makeRow(val); });
}
var $body = $('tbody');
var functionArray = [
unformatParseFloat,
unformatRegex
];
var values = {
us: [
'123.45',
'123456.78',
'123,456.78',
'12,123.12',
'12,123.00',
'1,234.56',
'123,456.789',
'123,456.11'
],
other: [
'123,45',
'123456,78',
'123.456,00',
'123.345.232,234'
]
};
$(document).ready(function(){
$body = $('tbody');
processValues();
});