Javascript 循环遍历字符串以查找多个索引
我正试图找出一种最有效的方法来遍历字符串并找到某个字母的所有索引 我使用了Javascript 循环遍历字符串以查找多个索引,javascript,jquery,loops,indexing,indexof,Javascript,Jquery,Loops,Indexing,Indexof,我正试图找出一种最有效的方法来遍历字符串并找到某个字母的所有索引 我使用了$word\u或短语.indexOf($letter)查找字母的单个索引,但该字母多次出现在$word\u或\u短语中。最有效的方法是构建一个包含所有索引的数组,直到.indexOf返回-1吗?或者你建议我如何找到所有的索引 我已经花了一些时间,发现了这一点: 这是可行的,但对我来说,在处理超过2个索引时似乎效率不高。如果我有10个呢 提前谢谢你的建议 var mystring='hello world'; var my
$word\u或短语.indexOf($letter)
查找字母的单个索引,但该字母多次出现在$word\u或\u短语中。最有效的方法是构建一个包含所有索引的数组,直到.indexOf
返回-1吗?或者你建议我如何找到所有的索引
我已经花了一些时间,发现了这一点:
这是可行的,但对我来说,在处理超过2个索引时似乎效率不高。如果我有10个呢
提前谢谢你的建议 var mystring='hello world';
var mystring = 'hello world';
var letterToCount = 'l';
var indexes = [];
for(var i=0; i<mystring.length; i++) {
if(mystring[i] == letterToCount)
indexes.push(i);
}
alert(indexes.join(',')); //2,3,9
var letterToCount='l';
var指数=[];
对于(var i=0;i请这样尝试
var str = "foodfoodfoodfooooodfooooooood";
for (var index = str.indexOf("o");index > 0; index = str.indexOf("o", index+1)){
console.log(index);
}
正如您发布的StackOverflow链接中的答案所示,您可以使用indexOf
的第二个参数来定义搜索在字符串中开始的位置。您可以使用此技术继续在字符串上循环,以获得所有匹配子字符串的索引:
function getMatchIndexes(str, toMatch) {
var toMatchLength = toMatch.length,
indexMatches = [], match,
i = 0;
while ((match = str.indexOf(toMatch, i)) > -1) {
indexMatches.push(match);
i = match + toMatchLength;
}
return indexMatches;
}
console.log(getMatchIndexes("asdf asdf asdf", "as"));
演示:
另一个选项是使用正则表达式查找所有匹配项:
function getMatchIndexes(str, toMatch) {
var re = new RegExp(toMatch, "g"),
indexMatches = [], match;
while (match = re.exec(str)) {
indexMatches.push(match.index);
}
return indexMatches;
}
console.log(getMatchIndexes("asdf asdf asdf", "as"));
演示:
还有一个选项是手动循环字符串的字符并与目标进行比较:
function getMatchIndexes(str, toMatch) {
var re = new RegExp(toMatch, "g"),
toMatchLength = toMatch.length,
indexMatches = [], match,
i, j, cur;
for (i = 0, j = str.length; i < j; i++) {
if (str.substr(i, toMatchLength) === toMatch) {
indexMatches.push(i);
}
}
return indexMatches;
}
console.log(getMatchIndexes("asdf asdf asdf", "as"));
函数getMatchIndexes(str、toMatch){
var re=新的RegExp(toMatch,“g”),
toMatchLength=toMatch.length,
indexMatches=[],匹配,
i、 j,cur;
对于(i=0,j=str.length;i
演示:可能是一个解决方案:
检查这个
var data = 'asd 111 asd 222 asd 333';
var count = countOccurence('asd', data);
console.info(count);
function countOccurence(item, data, count){
if (count == undefined) { count = 0; }
if (data.indexOf(item) != -1)
{
count = count+1;
data = data.substring(data.indexOf(item) + item.length);
count = countOccurence(item, data, count);
}
return count;
}
感谢sjkm的快速响应,但是,这并没有给出索引,而是给出了字母存在的次数。在您的示例中,我希望输出的是发生率=3,4,9。我没有否决您的记录。请执行上面的代码…它返回您想要的内容^^^这看起来很棒!谢谢Dineshkani!我没有ot否决你的记录。它正在经历无限的迭代。谢谢你的回复!谢谢你给大家提供的快速和有用的建议。有足够多的贡献来回答这个问题,这篇文章肯定会对未来的人有所帮助。你会如何找到例如“ea”我的意思是,你只能找到一个字母的匹配。你怎么能用两个以上的字母来做呢?
var data = 'asd 111 asd 222 asd 333';
var count = countOccurence('asd', data);
console.info(count);
function countOccurence(item, data, count){
if (count == undefined) { count = 0; }
if (data.indexOf(item) != -1)
{
count = count+1;
data = data.substring(data.indexOf(item) + item.length);
count = countOccurence(item, data, count);
}
return count;
}