0

请看下面的代码

<!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;

    //Split the words
    listOfWordsArray = listOfWords.split("\n");

    //Convert the entire word list to upper case
    for(var i=0;i<listOfWordsArray.length;i++)
    {
        listOfWordsArray[i] = listOfWordsArray[i].toUpperCase();
    }

    //Get the paragrah text
    paragraph = document.getElementById("paragraph").value;
    paragraphArray = paragraph.split(" ");

    //Convert the entire paragraph to upper case
    for(var i=0; i<paragraphArray.length; i++)
    {
        paragraphArray[i] = paragraphArray[i].toUpperCase();
    }

    //check whether paragraph contains words in list
    for(var i=0; i<listOfWordsArray.length; i++)
    {
    /*  if(paragraph.contains(listOfWords[i]))
        {
                wordCounter++;
        }*/

        re = new RegExp("\\b"+listOfWordsArray[i]+"\\b");

        if(paragraph.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其中包含多少个单词wordList。中的单词wordList用换行符分隔。

但是,我需要此检查不区分大小写。例如,“count”、“COUNT”和“Count”之间应该没有区别。

但是在这里,我总是得到答案 0。我在这里做错了什么?

更新

我尝试了 SO User 'Kolink' 提供的以下功能。然而,它在不同的运行中给出了不同的答案。在最初的几次运行中它是正确的,然后它开始提供错误的答案!也许 JavaScript 作为static变量?

4

4 回答 4

2

您正在准备段落中的文字,paragraphArray但您从不使用它。

我会建议这样的事情:

var words = document.getElementById('wordsList').value.split(/\r?\n/),
    l = words.length, i, total = 0, para = document.getElementById('paragraph').value;
for( i=0; i<l; i++) if( para.match(new RegExp("\\b"+words[i]+"\\b","i"))) total++;
alert("Total: "+total);
于 2013-09-04T15:10:15.770 回答
1

解决方案

这个怎么样:

var wc = function (text, wordsToMatch) {
  var re = new RegExp("(" + (wordsToMatch || ["\\w+"]).join('|') + ")", "gi");
  var matches = (text || "").match(re);

  // console.log(matches);
  return (matches ? matches.length : 0);
};

或者对于不可读的版本(不推荐):

var wc = function (t, w) {
  return (((t || "").match(new RegExp("(" + (w || ["\\w+"]).join('|') + ")", "gi")) || []).length);
};

一体化

因此,在您的代码中,您可以丢弃大部分代码并编写:

function count()
{
    var wordsList   = document.getElementById("wordsList").value;
    var paragraph   = document.getElementById("paragraph").value;
    var wordCounter = wc(paragraph, wordsList.split("\n"));

    window.alert("Number of Contains: " + wordCounter);
}

例子

示例 1(与列表匹配)

输入:

console.log(wc("helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld", ["world"]));
console.log(wc("helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld", ["hello", "world"]));

输出:

12
24

示例 2(安全默认值)

输入:

console.log(wc("", ["hello", "world"]));
console.log(wc());
console.log(wc(""));

输出:

0
0
0

示例 3(作为默认字数计数器)

输入:

console.log(wc("hello"));
console.log(wc("hello world"));

输出:

1
2
于 2013-09-04T15:26:47.037 回答
0

您可以不使用正则表达式进行搜索(链接到eliminateDuplicates):

var wordCounter = 0;

// retrieves arrays from textareas

var list = eliminateDuplicates(
    document.getElementById('wordsList').value
    .toUpperCase()
    .split(/\s+/g)
);
var para = eliminateDuplicates(
    document.getElementById('paragraph').value
    .toUpperCase()
    .split(/\s+/g)
);

// performs search

for (var i1 = 0, l1 = para.length; i1 < l1; i1++) {
    var word = para[i1];
    for (var i2 = 0, l2 = list.length; i2 < l2; i2++) {
        if (list[i2] === word) {
            wordCounter++;
            break;
        }
    }
}
于 2013-09-04T15:32:28.933 回答
-2

您的正则表达式格式不正确。尝试

re = new RegExp("\\b"+listOfWordsArray[i]+"\b\");

因为第一个字符是 \ ,所以最后一个应该是 \ ,而不是 b

于 2013-09-04T15:12:44.560 回答