Can anyone help me with this? about finding the number of same words

/*
Write a function called countChars that accepts two parameters: a string and a character. This function should return a number representing the number of times that the character appears in string. To access the first element of a string, you can use the following syntax:

access the element at index 0
“hello”[0]; // => “h”
“dog”[0]; // => “d”

HINT: You’ll also need to make use of the slice method
*/

It’s a simple question I know but what I do from now was

function countChars(string, character) {
var pattern = new RegExp(character);
return (string.match(pattern).length);
}

countChars(‘doggie’,‘g’);
countChars(‘terrarium’,‘r’);

it yields 1,1 .

can anyone help me how to solve this?

Hint: You need to look into regex global flag.
Hint2: You’re really really close - couple of keystrokes away
Hint3: You don’t need slice or to access elements with [ ] bracket notation

Good luck!