不区分大小写的字符串搜索不起作用
Case insensitive string search is not working
请查看以下代码
<!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
中。CCD_ 3中的字由新行分隔。
但是,我需要此检查不区分大小写。例如,"count"、"count"answers"count"之间不应有区别。
但在这里,我总是得到答案0。我在这里做错了什么?
更新
我尝试了SO用户"Kolink"提供的以下功能。然而,它在不同的运行中给出了不同的答案。在最初的几次运行中,它是正确的,然后它开始提供错误的答案!也许JavaScript是static
变量?
您正在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);
解决方案
这个怎么样:
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
您可以不使用regexp进行搜索(链接到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;
}
}
}
您的正则表达式格式不正确。尝试
re = new RegExp("''b"+listOfWordsArray[i]+"'b'");
因为第一个字符是'',所以最后一个应该是'',而不是b
相关文章:
- jQuery Datatables:单个列搜索不起作用
- 搜索按钮谷歌地图在我的vf页面上不起作用
- Angular UI Select2指令搜索功能不起作用
- jQuery .focus() 在 Safari(桌面)的搜索字段中不起作用
- 在js中的表中搜索不起作用
- AngularJS搜索没有'不起作用
- 在引导选择组件中的搜索不起作用,其中数据令牌包含所有数字;4730”;
- JQuery UI 地图搜索不起作用
- 谷歌地图地点搜索框不起作用
- 搜索按钮不起作用
- jQuery搜索和替换不起作用(Wordpress)
- 不区分大小写的字符串搜索不起作用
- 引导表分页和搜索不起作用
- ajax实时建议搜索不起作用
- jQuery mobile折叠列表视图,搜索不起作用
- Angular在ng-repeat中的搜索不起作用
- 实时搜索不起作用
- 选择2 4.0 JSON搜索不起作用
- JS,在数组中搜索不起作用
- 为什么我的即时搜索不起作用