DEV Community

Discussion on: Advanced splitting with Symbol.split

Collapse
 
aminnairi profile image
Amin • Edited

Hi Matt and thanks for your answer.

I didn't knew myself we could do that with the String.prototype.match method. Not only this is easier, but it is also faster (at least for Google Chrome) than what I did with the Array.prototype.reduce method.

But I guess we could simplify the RegExp and the fuction to one line if we wanted to to aggressive optimizations.

const splitWithDelimiter = (delimiter, string) => string.match(new RegExp(`(${delimiter}|[^${delimiter}]+)`, "g"));
Enter fullscreen mode Exit fullscreen mode

Since it does not need to check for characters before the slash (in this case since we are matching a UNIX absolute path).