function extractNumber(val) {
// I need another way, to try to solve this function, this way I'm using it didn't work correctly //
return val.replace(/[^\d]+/g,'')
}
Examples of what the result should look like:
'oo' => NaN
'57o' => 57
'n1.5' => 1.5
'n1,5' => 15
Based on the examples you provided, you could do something like this:
function extractNumber(val) {
return val.replace(/^(-)|[^0-9.]+/g, '$1') || 'NaN'
}
let strings = ['oo', '57o', 'n1.5', 'n1,5']
strings.forEach(s => {
console.log(extractNumber(s))
})
const extractNumber = (val) => val.replaceAll(/[^\d]/g, "");
example:
> extractNumber("ab23b1b512b12")
'23151212'