不区分大小写的字符串搜索不起作用

时间:2013-09-04 15:05:32

标签: javascript html web

请查看以下代码

<!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 static中的单词用新行分隔。

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

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

更新

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

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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

答案 2 :(得分:0)

您可以在没有正则表达式(link to 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;
        }
    }
}

答案 3 :(得分:-2)

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

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

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