请看下面的代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function count()
{
var listOfWords, paragraph, listOfWordsArray, paragraphArray;
var wordCounter=0;
listOfWords = document.getElementById("wordsList").value;
listOfWords = listOfWords.toUpperCase();
//Split the words
listOfWordsArray = listOfWords.split("/\r?\n/");
//Get the paragrah text
paragraph = document.getElementById("paragraph").value;
paragraph = paragraph.toUpperCase();
paragraphArray = paragraph.split(" ");
//check whether paragraph contains words in list
for(var i=0; i<paragraphArray.length; i++)
{
re = new RegExp("\\b"+paragraphArray[i]+"\\b","i");
if(listOfWordsArray.match(re))
{
wordCounter++;
}
}
window.alert("Number of Contains: "+wordCounter);
}
</script>
</head>
<body>
<center>
<p> Enter your Word List here </p>
<br />
<textarea id="wordsList" cols="100" rows="10"></textarea>
<br />
<p>Enter your paragraph here</p>
<textarea id="paragraph" cols="100" rows="15"></textarea>
<br />
<br />
<button id="btn1" onclick="count()">Calculate Percentage</button>
</center>
</body>
</html>
我正在尝试遍历paragraph
并检查段落中有多少单词在listOfWords
. 这不应该省略单词重复,这意味着如果段落中有2个或多个相同的单词(例如:2个“农民”单词),则应将其视为2个单词并计数,不应因悔改而省略。
现在,我的代码没有提供任何输出,我不知道为什么。