Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 718
Nikola Dim Avatar asked Jul 31 '19 16:07

Nikola Dim


People also ask

Can we use index in Split?

Splitting Strings ] Now that we have a new array in the splitString variable, we can access each section with an index number. If an empty parameter is given, split() will create a comma-separated array with each character in the string.

How do you get a specific index of a string in JavaScript?

To get the index of a character in a string, you use the indexOf() method, passing it the specific character as a parameter. This method returns the index of the first occurrence of the character or -1 if the character is not found.

Does split return an array JavaScript?

split() The split() method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.

What does .split do in JavaScript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.


1 Answers

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.

like image 188
melpomene Avatar answered Nov 11 '22 10:11

melpomene