te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - get indexes for split string - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - get indexes for split string - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 4

Use 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"], ...]
发布评论

评论列表(0)

  1. 暂无评论