如何使用str.split()函数获取匹配的索引数组而不是实际匹配?
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.
更好的是,这个2D阵列输出是理想的:
//[[0, "The"], [4, "quick"], [10, "brown"], [16, "fox"], [20, "jumps"], [26, "over"], ...]
答案 0 :(得分:3)
如果所有单词都是唯一的,您可以这样做:
示例: http://jsfiddle.net/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] );
}
如果有重复的话,应该这样做:
示例: http://jsfiddle.net/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 );
}
答案 1 :(得分:3)
使用此方法:
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
}
示例:
alert(splitWithIndex(str,' '))
EDIT(2018年12月17日):避免向本机String对象添加方法。
答案 2 :(得分:2)
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
答案 3 :(得分:1)
以下方法是对字符串进行简单的线性扫描。它比split()和indexOf()的组合更快。此外,它产生完整的“2D”结果(BTW,问题中的编号不正确)。
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"], ...]