简体   繁体   English

匹配2个字符串

[英]Matching 2 strings

Please have a look at the following code 请看下面的代码

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

I am trying to iterate through the paragraph and check how many words in paragraph are inside the listOfWords . 我试图遍历该paragraph并检查listOfWords有多少个单词。 This should not omit words repetition , which means if there are 2 or more same words (ex: 2 "farmer" words) in paragraph, it should take it as 2 words and count and should not omit it because of the repentance. 这不应该忽略重复单词,这意味着如果段落中有2​​个或更多相同的单词(例如:2个“农民”单词),则应将其视为2个单词并计数,并且不要因为悔改而忽略它。

Right now, my code is not providing any output, I don't know why. 现在,我的代码没有提供任何输出,我不知道为什么。

You are looking for a string to split, not a regular expression 您正在寻找要拆分的字符串,而不是正则表达式

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

You do not want the quotes 您不希望引号

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

All the issues with the code that have been pointed out aside, I'd personally avoid the regex altogether and just run a check on the index: 除了上面已经指出的所有代码问题外,我个人完全避免使用正则表达式,而只对索引进行检查:

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);
}

First remove the quotes around the regex. 首先删除正则表达式周围的引号。 Then try something like 然后尝试类似

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

instead of 代替

if(listOfWordsArray.match(re))
{
    wordCounter++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM