summaryrefslogtreecommitdiff
path: root/util.js
blob: 934ccf5215052f3cd008c401ddeea8e754c6d5cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* Adapted from: https://stackoverflow.com/a/45502134/5401763 under CC BY-SA 4.0 */
exports.splitSegments = function (string, pattern) {
  let segments = [];
  let match;
  let lastIndex = 0;
  pattern.lastIndex = 0;

  while (match = pattern.exec(string)) {
	if (match.index > lastIndex) {
	  segments.push(string.substring(lastIndex, match.index));
	}
	segments.push({ match });
	lastIndex = match.index + match[0].length;
  }
  if (lastIndex < string.length) {
	segments.push(string.substr(lastIndex));
  }
  return segments;
};