小编Kat*_*rin的帖子

如何从关键字搜索中返回最短的片段?

我有以下任务要解决:

给定一页包含字母数字单词的内容,以及一个包含 N 个单词的搜索短语,编写一个算法,该算法将返回包含所有 N 个单词的最短内容片段(以任何顺序排列)。

这是我到目前为止。我将页面上的文本放入一个没有标点符号的数组中:

var allText = $('#all-text').text();
var punctuationless = allText.replace(/[^A-Za-z0-9_]/g," ").toLowerCase();
var lessSpaces = punctuationless.replace(/\s{2,}/g," ");
var allTextArray = lessSpaces.split(" ");
var keywords = [];
Run Code Online (Sandbox Code Playgroud)

我想我可以使用 .filter 方法,但我不确定如何比较两个数组。

allTextArray.filter(keywords)
//find the indexes of the matches to the keywords
//compare how far apart the indexes are to each other and return the shortest length
Run Code Online (Sandbox Code Playgroud)

javascript

5
推荐指数
0
解决办法
279
查看次数

标签 统计

javascript ×1