How to count the number digits in a string with Node.js

2 Answers

0 votes
const str = '824 javascript 9001 c c++ 17';
 
const count = (str.match(/[0-9]/g) || []).length;
 
console.log(count); 
 
   
   
   
   
/*
run:
   
9
   
*/

 



answered May 4, 2022 by avibootz
0 votes
const str = '824 javascript 9001 c c++ 17';
 
const count = str.replace(/[^0-9]/g, "").length;
 
console.log(count); 
 
   
   
   
   
/*
run:
   
9
   
*/

 



answered May 4, 2022 by avibootz
...