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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With