diff --git a/chapters/strings/finding-substrings.md b/chapters/strings/finding-substrings.md index a33af8c..5a070d2 100644 --- a/chapters/strings/finding-substrings.md +++ b/chapters/strings/finding-substrings.md @@ -24,8 +24,14 @@ message.indexOf "This", 5 message.lastIndexOf "This" # => 49 -{% endhighlight %} -## Discussion +# Count occurrences of a given string + + substrCount = (str, subStr) -> + re = new RegExp( subStr ,"g" ) + ( str.match(re) or [] ).length -Still need recipe to count occurrences of a given string within a message. +substrCount message, " a " +# => 3 + +{% endhighlight %}