[SOLVED] JS split string and return index of each split


I want to split text on a certain regex and also to have an index of where that split starts in the original string.
On a simple example:

"bla blabla haha".splitOnRegexWithIndex(whitespaceRegex)

Needed output is

[["bla", 0], ["blabla", 4], ["haha", 11]]

Regex here can be anything, not just whitespace, so delimiter isn’t fixed size.

Splitting is done on regex. I don’t want to use indexOf to find "blabla" in the starting string because that would be O(n2) complexity which is not acceptable in my scenario.


Here’s a possible implementation based on .exec:

function split_with_offset(str, re) {
    if (!re.global) {
        throw "no no no no :(";
    let results = [];
    let m, p;
    while (p = re.lastIndex, m = re.exec(str)) {
        results.push([str.substring(p, m.index), p]);
    results.push([str.substring(p), p]);
    return results;

console.log(split_with_offset("bla blabla haha", /\s+/g));
console.log(split_with_offset("   ", /\s+/g));
console.log(split_with_offset("", /\s+/g));

Caveat: The regex must have the g flag set.

Answered By – melpomene

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published.