Quantcast
Viewing all articles
Browse latest Browse all 43

Answer by Rebecca Chernoff for How to count string occurrence in string?

The g in the regular expression (short for global) says to search the whole string rather than just find the first occurrence. This matches is twice:

var temp = "This is a string.";var count = (temp.match(/is/g) || []).length;console.log(count);

And, if there are no matches, it returns 0:

var temp = "Hello World!";var count = (temp.match(/is/g) || []).length;console.log(count);

Viewing all articles
Browse latest Browse all 43

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>