UNPKG

@simongrasinovski/str-utils

Version:

A lightweight utility library for effortless string manipulation. Simplify common tasks like case validation, conversion and formatting with easy-to-use functions that enhance your JavaScript applications

23 lines (21 loc) 685 B
"use strict"; var _require = require('./utils'), ensureString = _require.ensureString; /** * Counts occurrences of a substring within a string. * * @function * @param {string} input - The string to search. * @param {string} substring - The substring to count. * @returns {number} Count of occurrences. * * @throws {TypeError} Throws an error if the input is not a string. * * @example * const result = countOccurrences('hello world, hello from stringUtils', 'hello'); * console.log(result); // 2 */ module.exports = ensureString(function (input, substring) { var result = input.match(new RegExp(substring, 'g')); return result ? result.length : 0; });