How to use 'javascript count occurrences in string' in JavaScript

Every line of 'javascript count occurrences in string' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
299function occurrences(string, subString) {
300 var n = 0, pos = 0, step = subString.length
301
302 while (true) {
303 pos = string.indexOf(subString, pos)
304 if (pos >= 0) {
305 n++
306 pos += step
307 }
308 else break
309 }
310 return n
311}

Related snippets