Version: v0.7.1 - Beta.  We welcome contributors & feedback.

String.count

count($matchString, $options = {}) -> number

Description

Return the number of times $matchString occurs in the string.

If $options is -ignoreCase it will match both upper and lowercase versions of each character.

'a xx b xx c XX'.count('xx')
//= 2

'ABC abc'.count('abc', -ignoreCase)
//= 2

Regex Patterns

If you need to count instances of a pattern, you can use matchAll instead.

'abc def'.matchAll(r'\w+').length()
//= 2

// i = ignore case
'Abc Def Xyz'.matchAll(r'(abc|xyz)'i).length()
//= 2

See Also