function splitKeepDelims(s: string, delimiters: string): string[] {
const result: string[] = [];
// Build regex: e.g. ",;|" → /([,;|])/g
const escaped = delimiters.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const pattern = new RegExp(`([${escaped}])`, "g");
let lastEnd = 0;
for (const match of s.matchAll(pattern)) {
const index = match.index!;
const delim = match[0];
// Add text before delimiter
if (index > lastEnd) {
result.push(s.substring(lastEnd, index));
}
// Add the delimiter itself
result.push(delim);
lastEnd = index + delim.length;
}
// Add remaining text after last delimiter
if (lastEnd < s.length) {
result.push(s.substring(lastEnd));
}
return result;
}
const parts = splitKeepDelims("aa,bbb;cccc|ddddd", ",;|");
for (const p of parts) {
process.stdout.write(`[${p}] `);
}
/*
run:
[aa] [,] [bbb] [;] [cccc] [|] [ddddd]
*/