Method 1: split()
the string at the occurrences of the character/pattern and add 1 to the length
of the resulting array
Method 2: Create an array from the result of matchAll()
and get its length
str = 'All is well and ends well';
char = 'l';
str.split(char).length - 1;
//OR this works too
[...str.matchAll(char)].length
Top comments (0)