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
this disclaimer
299function occurrences(string, subString) {
300 var n = 0, pos = 0, step = subString.length
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

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

Related snippets