Create a function, or show a built-in function, to count the number of non-overlapping occurrences of a substring inside a string.
The function should take two arguments:
the first argument being the string to search, and the second a substring to be searched for.It should return an integer count.
The matching should yield the highest number of non-overlapping matches.
In general, this essentially means matching from left-to-right or right-to-left.
countSubstring
is a function.
testString: assert(typeof countSubstring === 'function', 'countSubstring
is a function.');
- text: countSubstring("the three truths", "th")
should return 3
.
testString: assert.equal(countSubstring(testCases[0], searchString[0]), results[0], 'countSubstring("the three truths", "th")
should return 3
.');
- text: countSubstring("ababababab", "abab")
should return 2
.
testString: assert.equal(countSubstring(testCases[1], searchString[1]), results[1], 'countSubstring("ababababab", "abab")
should return 2
.');
- text: countSubstring("abaabba*bbaba*bbab", "a*b")
should return 2
.
testString: assert.equal(countSubstring(testCases[2], searchString[2]), results[2], 'countSubstring("abaabba*bbaba*bbab", "a*b")
should return 2
.');
```