计算句子中出现的字符数

时间:2018-03-26 16:05:44

标签: javascript html

我正在编写一个代码,用户可以输入任何文本并选择任何字母来查看该特定文本中出现的次数 - 我不确定我哪里出错了

function textOccurrences() {
  var inputField1 = (document.getElementById("inputField1").value);
  var inputField2 = (document.getElementById("inputField2").value);
  var count = 0;

  for (i = 0; i < length; i++) {
    if (parseInt(search) != -1) {
      count++;
      var search = inputField1.indexOf(inputField2, parseInt(search) + 1);
    }
    document.getElementId("answer").value = inputField2 + "Occurs" + count + "times";
  }
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title> Text Occurrences </title>
  <h1> Number of Character Occurrences</h1>
  <script="text/javascript"> </script>
</head>

<body>
  <form>
    <p>Enter text: </p>
    <p> <input id="inputField1" type="text" /> </p>

    <p>Enter any character:</p>
    <p> <input id="inputField2" type="text" /> </p>
    <input type="button" value="Search" onlick="textOccurrences()" />
    </p>
    <p>Number of Occurrences:</p>
    <p><textarea id="answer"></textarea></p>
  </form>
</body>

</html>

4 个答案:

答案 0 :(得分:0)

尝试将此作为您的功能。

function textOccurrences() {
  var inputField1 = (document.getElementById("inputField1").value);
  var inputField2 = (document.getElementById("inputField2").value);
  var count = inputField1.split(inputField2).length - 1;
  document.getElementId("answer").value = inputField2 + "Occurs" + count + "times";
}

答案 1 :(得分:0)

您可以使用正则表达式:

function textOccurrences() {
  var inputField1 = (document.getElementById("inputField1").value);
  var inputField2 = (document.getElementById("inputField2").value);
  var re = new RegExp(inputField2, "g");
  var count = inputField1.match(re).length;
  document.getElementById("answer").value = inputField2 + " Occurs " + count + " times";
}

请注意,在您的代码中,您说的是getElementId而不是getElementById,这也是导致错误的原因。

答案 2 :(得分:0)

function textOccurrences() {
  var inputField1 = (document.getElementById("inputField1").value);
  var inputField2 = (document.getElementById("inputField2").value);
  var reg = new RegExp(inputField2,'g');
  document.getElementById("answer").value = inputField2 + "Occurs" + inputField1.match(reg).length + 
  "times";
}

您可以尝试这样的事情

答案 3 :(得分:0)

你可以使用正则表达式匹配你所选择的任何字母的出现,然后计算这些匹配,你必须在你的HTML和JavaScript中出现语法错误
- 是onclickonlick
- getElementByIdgetElementId

这是编辑后的代码 的的Javascript

function textOccurrences() {
   var inputField1 = (document.getElementById("inputField1").value);
   var inputField2 = (document.getElementById("inputField2").value);
   console.log("hekasdlkjasd");
   var re = new RegExp(inputField2, "g");
   var count = inputField1.match(re).length;
   document.getElementById("answer").value = inputField2 + " Occurs " + count + " times";

}

<强> HTML

 <!DOCTYPE html>
 <html>
    <head>
        <title> Text Occurrences </title>
        <h1> Number of Character Occurrences</h1>
        <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    </head>
 <body>
   <form>
       <p>Enter text: </p>
       <p> <input id="inputField1" type="text" /> </p>

       <p>Enter any character:</p>
       <p> <input id="inputField2" type="text" /> </p>
       <input type="button" value="Search" onclick="textOccurrences();" />
       </p>
       <p>Number of Occurrences:</p>
       <p><textarea id="answer"></textarea></p>
   </form>

 </body>
 </html>