How to count string occurrence in string?
The g in the regular expression (short for global) says to search the whole string rather than just find the first occurrence. This matches is twice: Expand snippet And, if there are no matches, it returns 0: