How to extract a float from a string in Node.js

1 Answer

0 votes
const text = "The float number is 8932.506 F";
const floatRegex = /[-+]?\d*\.\d+|\d+/;
const match = text.match(floatRegex);

if (match) {
    const number = parseFloat(match[0]);
    console.log("Extracted float:", number);
}


 
/*
run:

Extracted float: 8932.506
      
*/

 



answered Jul 29, 2025 by avibootz
...