最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get all occurrences of a substring in a very big string - Stack Overflow

programmeradmin2浏览0评论

Something like indexOf, except I need to find all indexes. If there's .indexOf and ,lastIndexOf , shouldn't there be a function to get all index occurences? I couldn't find.

Note that the string is very large, about 1MB in size, so I would need the fastest solution.

To clarify, I need to get all positions of where a substring occurs in a string.

E.g.

var str = "foo bar foo bar"; //the real string is 1MB
var indexes = str.indexOfAll('foo'); //the function I need
console.log(indexes); //should print [0,8];

One thing that es to mind is to use indexOf in a recurring loop, finds the first word, cut the string at the index, then use indexOf again and so on until it finds nothing. I'm not sure about performance (cutting and re-creating large strings).

Something like indexOf, except I need to find all indexes. If there's .indexOf and ,lastIndexOf , shouldn't there be a function to get all index occurences? I couldn't find.

Note that the string is very large, about 1MB in size, so I would need the fastest solution.

To clarify, I need to get all positions of where a substring occurs in a string.

E.g.

var str = "foo bar foo bar"; //the real string is 1MB
var indexes = str.indexOfAll('foo'); //the function I need
console.log(indexes); //should print [0,8];

One thing that es to mind is to use indexOf in a recurring loop, finds the first word, cut the string at the index, then use indexOf again and so on until it finds nothing. I'm not sure about performance (cutting and re-creating large strings).

Share Improve this question edited Jun 4, 2019 at 14:52 Maciej Kravchyk asked Jun 4, 2019 at 14:49 Maciej KravchykMaciej Kravchyk 16.8k7 gold badges71 silver badges81 bronze badges 11
  • indexOf within loop – hindmost Commented Jun 4, 2019 at 14:51
  • Yeah I just thought about that edited my answer. But is there something more optimal? – Maciej Kravchyk Commented Jun 4, 2019 at 14:52
  • I would disagree about this duplicate. It is about searching for a string in an array, but this question is about searching for a string pattern, which can be achieved using string-search algorithms like Knuth-Morris-Pratt or Boyer-Moore. – Yeldar Kurmangaliyev Commented Jun 4, 2019 at 14:55
  • is there something more optimal? IMO loop with indexOf is most optimal. It doesn't cut/re-create strings. Furthermore, it doesn't make redundant iterations if one uses 2nd parameter of indexOf – hindmost Commented Jun 4, 2019 at 14:58
  • I'm going to try it and see how much it takes. I didn't know you can use indexOf in a loop just like that. If it takes a lot of time I'm going to try that string search algo. Thanks! – Maciej Kravchyk Commented Jun 4, 2019 at 15:01
 |  Show 6 more ments

2 Answers 2

Reset to default 5

Easy solution:

const str = "...";
const searchKeyword = "...";

const startingIndices = [];

let indexOccurence = str.indexOf(searchKeyword, 0);

while(indexOccurence >= 0) {
    startingIndices.push(indexOccurence);

    indexOccurence = str.indexOf(searchKeyword, indexOccurence + 1);
}

If you need something highly performant, you may look over specific text search/indexing algorithms like Aho–Corasick algorithm or Boyer–Moore string-search algorithm.

Really depends on your use case and if the text you're searching into is changing or is static and can be indexed beforehand for maximum performance.

const IndexString = (str1, str2, output=[]) =>{
  for(let i = 0; i < str1.length -1; i++){
    let arr = [];
    for(let j = i; j < (i+str2.length) & i<str1.length; j++) arr.push(str1[j])
    const findnew = arr.join('');
    if(findnew===str2) output.push(i);
  }
  return output
}

console.log(IndexString("tiktok tok tok tik tok tik", "tik"))

发布评论

评论列表(0)

  1. 暂无评论