不良词语捕捉器Javascript

4

我正在尝试创建一个应用程序,使用文本字段从用户那里获取输入。将字符串按空格分割并查找字符串中的不良词汇。如果找到不良词汇,则在DOM上输出结果。

我一直在寻找表单,但是没有找到。我找到了一些PHP表单,但这对我没有帮助。我相信我有正确的伪代码。需要一些指导。

以下是HTML:

<body>

<input type="text" id="wordInput"/>
    <button id="badWordCatch" onclick="badWordCatch;">Catch Bad Words</button>
    <div id="wordsFound"></div>
    <div id="wordAmount"></div>
</body>

Javascript如下:

    1. What words are put it
    2. if the words are bad or not
    */

    function badWordCatch(){

        var wordInput = document.getElementById("wordInput").value;

        // split the words by spaces (" ")
        var arr = wordInput.split(" ");
        // bad words to look for
        var badWords = ["legos", "cloud", "manifold"];


        //find bad words from the input
        //output on the dom "if bad words were found" 
        //output on the dom how many bad words were found
    }

可能只是一个打字错误,但是你实际上没有在提供的HTML中调用该函数。 - Will Ru
2个回答

4

您可以在arr上使用.filter来查看它是否包含badWords中的任何单词。

function badWordCatch() {

  var wordInput = document.getElementById("wordInput").value;
  wordInput = wordInput.toLowerCase();

  // split the words by spaces (" ")
  var arr = wordInput.split(" ");
  // bad words to look for, keep this array in lowercase
  var badWords = ["legos", "cloud", "manifold"];
  
  // .toLowerCase will do the case insensitive match!
  var foundBadWords = arr.filter(el => badWords.includes(el));
  
  document.getElementById("wordsFound").innerHTML = foundBadWords.join(", ");
  document.getElementById("wordAmount").innerHTML = foundBadWords.length;
  
  
}
<input type="text" id="wordInput" value="legos happy manifold" />
<button id="badWordCatch" onclick="badWordCatch()">Catch Bad Words</button>
<div id="wordsFound"></div>
<div id="wordAmount"></div>


1
你需要注意的一件事情是输入文本大小写可能发生变化的情况。因此,如果你想要检测不良词汇,就需要进行字符串大小写不敏感的比较。 - th3n3wguy
1
@th3n3wguy说得有道理!已编辑。 - void
@void 的 .filter 对我来说很有意义。很好的方法。你能解释一下 (el.toLowerCase())) 中的 "el." 吗? - king_coney
@king_coney el.toLowerCase() 将当前输入文本转换为小写,以便进行字符串不区分大小写的比较。您还可以将 .toLowerCase() 应用于 wordInput,例如 wordInput = wordInput.toLowerCase() - void
如果我想让DOM显示“发现了不良言论”或“未发现不良言论”,而不是打印出这些单词,我应该采取什么方法?使用if函数还是创建一个新变量并使用str.includes? - king_coney

0

您可以在 for 循环 中使用 jQuery.inArray

function badWordCatch(){

        var wordInput = document.getElementById("wordInput").value;

        // split the words by spaces (" ")
        var arr = wordInput.split(" ");
        // bad words to look for
        var badWords = ["legos", "cloud", "manifold"];
         var index , totalWordAmount = 0, totalWordsFoundList = '';
         for(index=0;index<arr.length; ++index){
         if(jQuery.inArray( arr[index], badWords ) > -1){
           totalWordAmount = totalWordAmount + 1;
           totalWordsFoundList = totalWordsFoundList+' '+ arr[index]; 
          // alert(arr[index])
         }
         //alert(totalWordsFoundList)
         }
         //alert(totalWordsFoundList)
         document.getElementById("wordsFound").innerHTML = totalWordsFoundList;
         document.getElementById("wordAmount").innerHTML = totalWordAmount;
          

        //find bad words from the input
        //output on the dom "if bad words were found" 
        //output on the dom how many bad words were found
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

<input type="text" id="wordInput"/>
    <button id="badWordCatch" onclick="badWordCatch();">Catch Bad Words</button>
    <div id="wordsFound"></div>
    <div id="wordAmount"></div>
</body>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接