How to extract the first number from string in TypeScript

3 Answers

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

 



answered May 4, 2022 by avibootz
0 votes
const s = "typescript412hjdsf72q1p0on8mq php 9953";
 
const n = s.match(/\d+/)[0];
 
console.log(n); 
 
   
     
     
/*
run:
     
"412" 
     
*/

 



answered May 4, 2022 by avibootz
0 votes
const s = "typescript412hjdsf72q1p0on8mq php 9953";
 
const arr = s.match(/^\d+|\d+\b|\d+(?=\w)/g);
  
console.log(arr[0]); 

   
     
     
/*
run:
     
"412" 
     
*/

 



answered May 4, 2022 by avibootz
...