Quantcast
Channel: How to count string occurrence in string? - Stack Overflow
Viewing all articles
Browse latest Browse all 43

Answer by Faraz Kelhini for How to count string occurrence in string?

$
0
0

The non-regex version:

 var string = 'This is a string',    searchFor = 'is',    count = 0,    pos = string.indexOf(searchFor);while (pos > -1) {++count;    pos = string.indexOf(searchFor, ++pos);}console.log(count);   // 2

Viewing all articles
Browse latest Browse all 43

Trending Articles



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