@spare/ripper
Version:
Split string by regex
23 lines (20 loc) • 484 B
JavaScript
const ripper = function (text) {
const regex = this;
let ms, l = 0, r = 0, sp, ph;
const vec = [];
while ((ms = regex.exec(text)) && ([ph] = ms)) {
r = ms.index;
if ((sp = text.slice(l, r))) vec.push(sp);
vec.push(ph);
l = regex.lastIndex;
}
if (l < text.length) vec.push(text.slice(l));
return vec
};
/**
*
* @param {RegExp} regex
* @return {Function}
*/
const Ripper = (regex) => ripper.bind(regex);
export { Ripper, ripper };