UNPKG

@modern-kit/utils

Version:
29 lines (26 loc) 861 B
const escapeRegExp = (str) => { const escaped = str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); return new RegExp(escaped, "g"); }; const countAllowOverlap = (source, target) => { const regex = escapeRegExp(target); let count = 0; let match = regex.exec(source); while (match !== null) { count++; regex.lastIndex = match.index + 1; match = regex.exec(source); } return count; }; const countExceptOverlap = (source, target) => { const regex = escapeRegExp(target); const matches = source.match(regex); return matches ? matches.length : 0; }; function countSubstringOccurrences(source, target, options = { overlap: false }) { if (target === "") return 0; return options.overlap ? countAllowOverlap(source, target) : countExceptOverlap(source, target); } export { countSubstringOccurrences }; //# sourceMappingURL=index.mjs.map