How to extract all numbers from string in JavaScript

2 Answers

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

 



answered Dec 17, 2020 by avibootz
edited Jan 30, 2022 by avibootz
0 votes
const s = "javascript7php412hjdsf72q1p0on8mq php 9953";
 
const arr = s.match(/^\d+|\d+\b|\d+(?=\w)/g);
 
console.log(arr); 
 
   
     
     
/*
run:
     
["7", "412", "72", "1", "0", "8", "9953"]
     
*/

 



answered Dec 17, 2020 by avibootz
edited Jan 30, 2022 by avibootz
...