试图只提取包含Javascript中特定单词的句子

时间:2018-05-15 19:07:49

标签: javascript arrays loops

我刚刚开始学习Javascript,而我正试图编写一种方法来提取和写入只包含文本中特定单词的句子。

这是我在下面的想法,但这显然不起作用,我找不到任何解决方案。你能给我一些语法建议吗?

function DetectConcl() {
  var Conclusions = ["so", "hence", "as a consequence"];
  var Entry = document.getElementById("enter").value;
  var EntryLow = Entry.toLowerCase();
  var Sentences = EntryLow.split(".");
  var Results = [];

  for (var i = 0; i < Conclusions.length; i++) {
    for (var j = 0; j < Sentences.length; j++) {
      if (Sentences[j].includes(Conclusions[i])) {
        Results.push(Sentences[j]);
      }
    }
  }

  for (var k = 0; k < Results.length; k++) {
    document.getElementById("conclusion").children[0].innerHTML += "<li>" + Results[k] + "</li>";
  }
}
<input id="enter" type="text">
<input type="button" value="Enter text" onclick="DetectConcl()">
<div id="conclusion">
  <ul></ul>
</div>

0 个答案:

没有答案