How to extract a float from a string in TypeScript

1 Answer

0 votes
const text = "The price is 148.95 dollars";
const floatRegex: RegExp = /[-+]?\d*\.\d+|\d+/;
const match: RegExpMatchArray | null = text.match(floatRegex);

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


 
/*
run:
     
"Extracted float:",  148.95 
      
*/

 



answered Jul 29, 2025 by avibootz
...