How to count the number of regex matches in Node.js

1 Answer

0 votes
const str = 'nodejs c++ nodejs nodejs c';

const count = (str.match(/nodejs/g) || []).length;

console.log(count); 

  
  
  
  
/*
run:
  
3
  
*/

 



answered May 4, 2022 by avibootz
...