How to split a string on multiple single‑character delimiters (and keep them) in TypeScript

2 Answers

0 votes
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]

*/

 



answered Mar 9 by avibootz
0 votes
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;
    let match: RegExpExecArray | null;

    while ((match = pattern.exec(s)) !== null) {
        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) {
    console.log(`[${p}] `);
}




/*
run:

"[aa] " 
"[,] " 
"[bbb] " 
"[;] " 
"[cccc] " 
"[|] " 
"[ddddd] " 

*/

 



answered Mar 9 by avibootz

Related questions

...