Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 匹配2个字符串_Javascript_Html_Web - Fatal编程技术网

Javascript 匹配2个字符串

Javascript 匹配2个字符串,javascript,html,web,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/

请看一下下面的代码

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

无标题文件
函数计数()
{
变量listOfWords,段落,listOfWordsArray,段落数组;
var-wordCounter=0;
ListoWords=document.getElementById(“wordsList”).value;
ListoWords=ListoWords.toUpperCase();
//分词
listOfWordsArray=listOfWords.split(“/\r?\n/”);
//获取paragrah文本
段落=document.getElementById(“段落”).value;
段落=段落.toUpperCase();
段落数组=段落分割(“”);
//检查段落是否包含列表中的单词

对于(var i=0;i您正在寻找要拆分的字符串,而不是正则表达式

listOfWordsArray = listOfWords.split("/\r?\n/");
您不需要引用

listOfWordsArray = listOfWords.split(/\r?\n/);

您正在寻找要拆分的字符串,而不是正则表达式

listOfWordsArray = listOfWords.split("/\r?\n/");
您不需要引用

listOfWordsArray = listOfWords.split(/\r?\n/);

抛开已经指出的所有代码问题不谈,我个人会完全避免使用正则表达式,只需对索引进行检查:

function count() {
  var listOfWords, paragraph, listOfWordsArray, paragraphArray, wordCounter; 
  wordCounter = 0;
  listOfWordsArray = document.getElementById('wordsList').value.toUpperCase().split(' ');
  paragraphArray = document.getElementById('paragraph').value.toUpperCase().split(' ');
  for (var i = 0, l = paragraphArray.length; i < l; i++) {

    if (listOfWordsArray.indexOf(paragraphArray[i]) >= 0) {
      wordCounter++;
    }

  }
  window.alert('Number of Contains: ' + wordCounter);
}
函数计数(){
变量listOfWords,段落,listOfWordsArray,段落数组,字计数器;
字计数器=0;
listOfWordsArray=document.getElementById('wordsList').value.toUpperCase().split(“”);
paragraphArray=document.getElementById('段落').value.toUpperCase().split('');
for(var i=0,l=paragraphArray.length;i=0){
wordCounter++;
}
}
window.alert('包含的数量:'+字计数器);
}

抛开前面提到的所有代码问题,我个人会完全避免使用正则表达式,只需对索引进行检查:

function count() {
  var listOfWords, paragraph, listOfWordsArray, paragraphArray, wordCounter; 
  wordCounter = 0;
  listOfWordsArray = document.getElementById('wordsList').value.toUpperCase().split(' ');
  paragraphArray = document.getElementById('paragraph').value.toUpperCase().split(' ');
  for (var i = 0, l = paragraphArray.length; i < l; i++) {

    if (listOfWordsArray.indexOf(paragraphArray[i]) >= 0) {
      wordCounter++;
    }

  }
  window.alert('Number of Contains: ' + wordCounter);
}
函数计数(){
变量listOfWords,段落,listOfWordsArray,段落数组,字计数器;
字计数器=0;
listOfWordsArray=document.getElementById('wordsList').value.toUpperCase().split(“”);
paragraphArray=document.getElementById('段落').value.toUpperCase().split('');
for(var i=0,l=paragraphArray.length;i=0){
wordCounter++;
}
}
window.alert('包含的数量:'+字计数器);
}

首先删除正则表达式周围的引号。然后尝试以下操作

for(var i=0;i<listOfWordsArray.length;i++)
{
    if(listOfWordsArray[i].match(paragraphArray[i])
    {
        wordCounter++;
    }
}

首先删除正则表达式周围的引号,然后尝试以下操作

for(var i=0;i<listOfWordsArray.length;i++)
{
    if(listOfWordsArray[i].match(paragraphArray[i])
    {
        wordCounter++;
    }
}

请始终在开发者控制台中检查错误!另外,一把小提琴也会很有帮助。我知道英语可能不是你的第一语言,但“悔改”的意思完全不同。我认为你想要的单词是“重复”?
匹配
不是
数组
的一种方法。使用firebug的简单检查可以告诉你这一点请在开发者控制台中检查错误!还有一把小提琴会很有帮助。我知道英语可能不是你的第一语言,但“悔改”的意思完全不同。我想你想要的单词是“重复”?
match
不是
Array
的方法。用firebug简单检查一下就可以告诉你这一点ify@epascarello的观点,不要在斜杠/引号之间包围值。在javascript中,reg ex仅由一个包含在无引号的正斜杠中的字符串指定。我也使用了您的建议。谢谢!为了澄清@epascarello的观点,不要在斜杠/引号之间包围值”。在javascript中,reg ex仅由一个包含在无引号的正斜杠中的字符串指定。我也使用了您的建议。谢谢!是的,这是答案。谢谢!是的,这是答案。谢谢!