How can I use the str.split() function to get an array of indexes of the matches instead of the actual matches?
e.g.:
var str = "The quick brown fox jumps over the lazy dog."
console.log(str.split(' '));
//["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."]
//I want to get this output instead for the index positions of the matches
//[0, 4, 10, 16, 20, 26, ...]
//01234567890123456789012345678901234567890123456789
//The quick brown fox jumps over the lazy dog.
Even better yet, this 2D array output would be ideal:
//[[0, "The"], [4, "quick"], [10, "brown"], [16, "fox"], [20, "jumps"], [26, "over"], ...]
How can I use the str.split() function to get an array of indexes of the matches instead of the actual matches?
e.g.:
var str = "The quick brown fox jumps over the lazy dog."
console.log(str.split(' '));
//["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."]
//I want to get this output instead for the index positions of the matches
//[0, 4, 10, 16, 20, 26, ...]
//01234567890123456789012345678901234567890123456789
//The quick brown fox jumps over the lazy dog.
Even better yet, this 2D array output would be ideal:
//[[0, "The"], [4, "quick"], [10, "brown"], [16, "fox"], [20, "jumps"], [26, "over"], ...]
Share
Improve this question
edited Jul 14, 2011 at 19:49
fortuneRice
asked Jul 13, 2011 at 23:27
fortuneRicefortuneRice
4,20411 gold badges45 silver badges60 bronze badges
1
- you will have to use a loop with conjunction of indexOf() – Ibu Commented Jul 13, 2011 at 23:30
4 Answers
Reset to default 4Use this method:
function splitWithIndex(str, delim){
var ret=[]
var splits=str.split(delim)
var index=0
for(var i=0;i<splits.length;i++){
ret.push([index,splits[i]])
index+=splits[i].length+delim.length
}
return ret
}
Example:
alert(splitWithIndex(str,' '))
EDIT (Dec. 17, 2018): Avoid adding methods to native String object.
If all the words are unique, you could do this:
Example: http://jsfiddle/rWJ5x/
var str = "The quick brown fox jumps over the lazy dog.";
var arr = str.split(' ');
for( var i = 0, len = arr.length; i < len; i++ ) {
arr[i] = str.indexOf( arr[i] );
}
If there are repeating words, this should do it:
Example: http://jsfiddle/rWJ5x/2/
var str = "The quick brown fox jumps over the lazy brown dog.";
var pos = 0;
var arr = str.split(' ');
for( var i = 0, len = arr.length; i < len; i++ ) {
var idx = str.indexOf( arr[i] );
arr[i] = pos = (pos + idx);
str = str.slice( idx );
}
function wordIndexes(s){
var A= [], rx= /([a-zA-Z']+)/g, M;
while((M= rx.exec(s))!= null){
A.push([M.index, M[1]]);
}
return A;
}
var string= 'The quick brown fox jumps over the lazy dog.';
wordIndexes(string).join('\n');
// returned value:
0, The
4, quick
10, brown
16, fox
20, jumps
26, over
31, the
35, lazy
40, dog
The following method is a simple linear sweep over the string. It is faster than the bination of split() and indexOf(). In addition it yields the plete "2D" result (BTW the numbering in the question is not correct).
function wordIndexes(str) {
var result = [];
var len = str.length;
var i = 0, j, word;
while (i < len) {
if (str[i] === ' ') {
++i;
}
else {
word = "";
for (j = i; j < len && str[j] !== ' '; ++j) {
word += str[j];
}
result.push([i, word]);
i = j;
}
}
return result;
}
var str = "The quick brown fox jumps over the lazy dog.";
// 01234567890123456789012345678901234567890123456789
var result = wordIndexes(str);
// => result = [[0, "The"], [4, "quick"], [10, "brown"], [16, "fox"], ...]