@ryusei/code
Version:
<div align="center"> <a href="https://code.ryuseijs.com"> <img alt="RyuseiCode" src="https://code.ryuseijs.com/images/svg/logo.svg" width="70"> </a>
18 lines (16 loc) • 533 B
text/typescript
/**
* Counts the search string occurrence in the provided sting.
*
* @param string - A string to search in.
* @param search - A string to search for.
* @param from - An index to search from.
* @param to - An index to search to.
*
* @return A number of occurrence.
*/
export function count( string: string, search: string, from = 0, to = string.length ): number {
if ( from || to !== string.length ) {
string = string.slice( from, to );
}
return ( string.match( new RegExp( search, 'g' ) ) || [] ).length;
}