How to extract all numbers from string including floats in JavaScript

2 Answers

0 votes
const s = "php412hjdsf72q1p0on8mq3.14 php 9953javascript200";
 
const arr = s.match(/\d+\.\d+|\d+\b|\d+(?=\w)/g)
  		     .map(function(n) {return + n;}); 
 
console.log(arr); 
 
   
     
     
/*
run:
     
[412, 72, 1, 0, 8, 3.14, 9953, 200]
     
*/

 



answered Dec 17, 2020 by avibootz
0 votes
const regex = /[+-]?\d+(\.\d+)?/g;

const s = "javascript7.351php412hjdsf72q1p0on8mq3.14 php 9953";

const arr = s.match(regex).map(function(v) { return parseFloat(v); });

console.log(arr);
  
    
      
    
    
/*
run:
      
[7.351, 412, 72, 1, 0, 8, 3.14, 9953]
      
*/

 



answered Jan 30, 2022 by avibootz
...