naive-ui
Version:
A Vue 3 Component Library. Fairly Complete, Theme Customizable, Uses TypeScript, Fast
23 lines (22 loc) • 776 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.splitAndMarkByRegex = splitAndMarkByRegex;
function splitAndMarkByRegex(str, regex) {
if (!regex.global) {
throw new Error('splitAndMarkByRegex requires a global regex (with "g" flag)');
}
const result = [];
let lastIndex = 0;
for (const match of str.matchAll(regex)) {
const { index } = match;
if (index > lastIndex) {
result.push({ text: str.slice(lastIndex, index), isMatch: false });
}
result.push({ text: match[0], isMatch: true });
lastIndex = index + match[0].length;
}
if (lastIndex < str.length) {
result.push({ text: str.slice(lastIndex), isMatch: false });
}
return result;
}